单独封装一个js(随即颜色的文件函数)
//随机颜色
function randomColor(){
var r = parseInt(Math.random()*255)
var g = parseInt(Math.random()*255)
var b = parseInt(Math.random()*255)
//'rgb(23, 45, 89)'
return 'rgb('+r+','+g+','+b+')'
}
1.图片轮播
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div>
<img src="" alt="" id="big"/>
</div>
<div id="small"></div>
<script type="text/javascript">
//图片对象构造方法
function YTImageModel(smallImage, bigImage, title=''){
this.smallImage = smallImage
this.bigImage = bigImage
this.title = title
}
imageArray = [
new YTImageModel('thumb-1.jpg', 'picture-1.jpg', '图片1'),
new YTImageModel('thumb-2.jpg', 'picture-2.jpg', '图片2'),
new YTImageModel('thumb-3.jpg', 'picture-3.jpg', 'iamge3'),
]
//大图节点
bigImgNode = document.getElementById('big')
bigImgNode.src = 'img/'+imageArray[0].bigImage
//小图盒子
smallBoxNode = document.getElementById('small')
//创建小图标
for(i=0; i<imageArray.length;i++){
//拿到图片模型
imageModel = imageArray[i]
//创建对应的小图节点
imageNode = document.createElement('img')
//设置图片
imageNode.src = 'img/'+ imageModel.smallImage
//***通过节点来保存图片模型
imageNode.imageMode = imageModel
//添加节点
smallBoxNode.appendChild(imageNode)
//添加事件
imageNode.addEventListener('mouseover', function(){
//使用鼠标点击的小图节点中保存的信息
bigImgNode.src = 'img/'+this.imageMode.bigImage
bigImgNode.title = this.imageMode.title
})
}
</script>
</body>
</html>
2,添加水果和删除
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<!--导入工具模块-->
<script type="text/javascript" src="js/tool.js"></script>
<style type="text/css">
.fruitBox{
width: 250px;
height: 60px;
background-color: cadetblue;
margin-bottom: 2px;
line-height: 60px;
}
.fruitName{
float: left;
width: 230px;
text-align: center;
font-size: 20px;
color: white;
}
.close{
font-size: 20px;
color: white;
/*设置光标样式*/
cursor: pointer;
}
/*输入框*/
#newFruit{
border: 0;
border-bottom: 1px solid #444444;
font-size: 30px;
width: 250px;
height: 50px;
text-align: center;
vertical-align: bottom;(同一级的居中)
}
#newFruit:focus{
outline: 0;
}
/*确定按钮*/
#sure{
border: 0;
background-color: coral;
color: white;
font-size: 20px;
width: 70px;
height: 30px;
}
#sure:focus{
outline: 0;
}
</style>
</head>
<body>
<div id="show"></div>
<div id='add'>
<input type="" name="" id="newFruit" value="" />
<button id="sure" onclick="addNewFruit()">确定</button>
</div>
<!--默认显示的水果-->
<script type="text/javascript">
//创建水果节点
function creatFruitNode(name){
//水果父节点
var divNode = document.createElement('div')
divNode.className = 'fruitBox'
//水果名字节点
var nameNode = document.createElement('font')
nameNode.innerText = name
nameNode.className = 'fruitName'
//关闭按钮
var closeNode = document.createElement('font')
closeNode.innerText = '×'
closeNode.className = 'close'
//关闭按钮添加点击事件
closeNode.addEventListener('click', function(){
//移除被点击的节点的父节点
this.parentElement.remove()
})
divNode.appendChild(nameNode)
divNode.appendChild(closeNode)
return divNode
}
fruitArray = ['苹果', '香蕉', '梨', '猕猴桃']
//展示所有水果的节点
showNode = document.getElementById('show')
for(index = 0; index < fruitArray.length; index++){
//拿水果名
fruitName = fruitArray[index]
//创建对应的节点
fruitNode = creatFruitNode(fruitName)
//添加水果节点
showNode.appendChild(fruitNode)
}
//点击确定按钮
function addNewFruit(){
//拿到输入的水果名
var newFruitName = document.getElementById('newFruit').value
//创建对应的节点
var newFruitNode = creatFruitNode(newFruitName)
newFruitNode.style.backgroundColor = randomColor()
//添加节点
showNode.insertBefore(newFruitNode, showNode.firstElementChild)
//清空输入框
document.getElementById('newFruit').value = ''
}
</script>
</body>
</html>
3闪烁方块代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/tool.js"></script>
<style type="text/css">
body{
text-align: center;
}
/*小盒子是60X60*/
#box{
width: 600px;
height: 300px;
margin-left: auto;
margin-right: auto;
margin-bottom: 5px;
border: 1px solid #666666;
}
/*按钮*/
button{
width: 60px;
height: 30px;
color: white;
background-color: red;
border: 0;
font-size: 16px;
}
button:focus{
outline: 0;
}
.newDiv{
width: 60px;
height: 60px;
float: left;
}
</style>
</head>
<body>
<div id="box"></div>
<button onclick="addAction()">添加</button>
<button onclick="blink()" id="blink">闪烁</button>
<script type="text/javascript">
boxNode = document.getElementById('box')
//添加
function addAction(){
//创建节点
newDivNode = document.createElement('div')
newDivNode.className = 'newDiv'
newDivNode.style.backgroundColor = randomColor()
//添加节点
boxNode.insertBefore(newDivNode, boxNode.firstElementChild)
//判断是否溢出
if(boxNode.children.length > 50){
//删除最后一个
boxNode.lastElementChild.remove()
}
}
//闪烁
function blink(){
//拿到按钮
blinkBtnNode = document.getElementById('blink')
//拿到所有的子标签
allSmallDiv = boxNode.children
if(blinkBtnNode.innerText == '闪烁'){
blinkBtnNode.innerText = '暂停'
//闪烁功能
timmer = setInterval(function(){
for(i=0; i<allSmallDiv.length;i++){
smallDivNode = allSmallDiv[i]
smallDivNode.style.backgroundColor = randomColor()
}
}, 100)
}else{
blinkBtnNode.innerText = '闪烁'
//暂停功能
clearInterval(timmer)
}
}
</script>
</body>
</html>