每天一个JS 小demo之韩雪冬轮播图。主要知识点:html,css布局,对于数组和对象的理解和运用

时间:2023-03-08 17:00:00
每天一个JS 小demo之韩雪冬轮播图。主要知识点:html,css布局,对于数组和对象的理解和运用

每天一个JS 小demo之韩雪冬轮播图。主要知识点:html,css布局,对于数组和对象的理解和运用

 @charset "utf-8";
/* CSS Document */ * { padding:; margin:; }
li { list-style: none; }
img { border: none; }
body { background: #ececec; padding-top: 50px; } #automatic { width: 970px; height: 344px; position: relative; margin: 0 auto; overflow: hidden; } .prev_div { width: 130px; height: 72px; position: absolute; top: 128px; left: 92px; z-index:; background: red; filter: alpha(opacity=0); opacity:; cursor: pointer; }
.next_div { width: 130px; height: 72px; position: absolute; top: 128px; right: 92px; z-index:; background: red; filter: alpha(opacity=0); opacity:; cursor: pointer; } #automatic .prev { width: 120px; height: 72px; position: absolute; top: 108px; left: 72px; z-index:; }
#automatic .prev .ico { width: 76px; height: 112px; position: absolute; top:; left:; background: url(images/prev.png); }
#automatic .prev .ico1 { width: 76px; height: 112px; position: absolute; top:; left:; background: url(images/prev_1.png); z-index:; filter: alpha(opacity=0); opacity:; }
#automatic .prev .txt { width: 106px; height: 112px; position: absolute; top:; left: 65px; background: url(images/prev_txt.png) no-repeat; filter: alpha(opacity=0); opacity:; } #automatic .next { width: 120px; height: 72px; position: absolute; top: 108px; right: 72px; z-index:; }
#automatic .next .ico { width: 76px; height: 112px; position: absolute; top:; right:; background: url(images/next.png) no-repeat; }
#automatic .next .ico1 { width: 76px; height: 112px; position: absolute; top:; right: 0px; background: url(images/next_1.png) no-repeat; z-index:; filter: alpha(opacity=0); opacity:; }
#automatic .next .txt { width: 106px; height: 112px; position: absolute; top:; right: 65px; background: url(images/next_txt.png) no-repeat; filter: alpha(opacity=0); opacity:; } #automatic ul { width: 970px; height: 344px; position: absolute; top:; left:; z-index:; }
#automatic li { position: absolute; } #automatic .line { border: 4px solid #fff; width: 672px; height: 336px; position: absolute; top:; left: 50%; margin-left: -340px; z-index:; } #automatic .pos_0 { top: -104px; left:; z-index:; filter: alpha(opacity=0); opacity:; }
#automatic .pos_1 { top: 104px; left:; z-index:; filter: alpha(opacity=60); opacity: 0.6; }
#automatic .pos_2 { top: 43px; left: 50px; z-index:; filter: alpha(opacity=80); opacity: 0.8; }
#automatic .pos_3 { top:; left: 145px; z-index:; }
#automatic .pos_4 { top: 43px; right: 50px; z-index:; filter: alpha(opacity=80); opacity: 0.8; }
#automatic .pos_5 { top: 104px; right:; z-index:; filter: alpha(opacity=60); opacity: 0.6; }
#automatic .pos_6 { top: -104px; right:; z-index:; filter: alpha(opacity=0); opacity:; } #footer { width: 970px; height: 30px; text-align: center; padding-top: 50px; margin: 0 auto; }
#footer a { color: #930; font-family: arial; font-size: 15px; text-decoration: none; border-bottom: 1px dotted #930; }
#footer a:hover { border-bottom: 1px solid #930; }
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>韩雪冬网站效果 - 妙味课堂 - www.miaov.com</title>
<!--[if lte IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>
<script type="text/javascript">
DD_belatedPNG.fix('span');
</script>
<![endif]-->
<link href="style.css" rel="stylesheet" type="text/css" />
</head> <body> <div id="automatic"> <div class="prev_div"></div>
<a class="prev" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a> <div class="next_div"></div>
<a class="next" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a> <div class="line"></div> <ul class="picList">
<li class="pos_0"><a href="http://www.miaov.com"><img src="data:images/8.jpg" width="100" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_1"><a href="http://www.miaov.com"><img src="data:images/1.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_2"><a href="http://www.miaov.com"><img src="data:images/2.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_3"><a href="http://www.miaov.com"><img src="data:images/3.jpg" width="680" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_4"><a href="http://www.miaov.com"><img src="data:images/4.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_5"><a href="http://www.miaov.com"><img src="data:images/5.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="data:images/6.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="data:images/7.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
</ul>
</div>
<p id="footer"><a href="http://www.miaov.com">妙味课堂 - www.miaov.com</a></p>
</body>
</html>
<script type="text/javascript" src="../../startmove.js"></script>
<script type="text/javascript">
/*初始宽度*/
// 使用函数自执行写法,防止全局污染
(function(){
var picList = document.querySelectorAll('.picList>li');
for(var i = 0; i <picList.length;i++){
// 为什么要加个[0]?
var img = picList[i].getElementsByTagName("img")[0];
css(picList[i],"width",img.width);
img.style.width = "100%";
}
})();
/* 左侧按钮的鼠标移入移出动画 */
(function(){
// 由于prev内部的元素层级都比它自身高,盖在它上面,所以如果在prev上加onmouseover或者onmouseout,实际上鼠标根本移动不到prev上,所以添加一个层级较高的prevBtn来防止干扰,nextBtn同理
var prevBtn = document.querySelector('.prev_div');
var prev = document.querySelector('.prev');
var prevSpan = prev.querySelectorAll('span');
prevBtn.onmouseover = function(){
startMove({
el: prevSpan[1],
target: {
left: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[0],
target: {
opacity:1,
left: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[2],
target: {
opacity:1,
left: 53
},
time: 300,
type: "easeBoth"
});
};
prevBtn.onmouseout = function(){
startMove({
el: prevSpan[1],
target: {
left: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[0],
target: {
opacity:0,
left: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[2],
target: {
opacity:0,
left: 65
},
time: 300,
type: "easeBoth"
});
};
})();
/* 右侧按钮的鼠标移入移出动画 */
(function(){
var nextBtn = document.querySelector('.next_div');
var next = document.querySelector('.next');
var nextSpan = next.querySelectorAll('span');
nextBtn.onmouseover = function(){
startMove({
el: nextSpan[1],
target: {
right: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[0],
target: {
opacity:1,
right: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[2],
target: {
opacity:1,
right: 53
},
time: 300,
type: "easeBoth"
});
};
nextBtn.onmouseout = function(){
startMove({
el: nextSpan[1],
target: {
right: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[0],
target: {
opacity:0,
right: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[2],
target: {
opacity:0,
right: 65
},
time: 300,
type: "easeBoth"
});
};
})();
/*
点击切换动画
*/
(function(){
var wrap = document.querySelector('#automatic');
var prevBtn = document.querySelector('.prev_div');
var nextBtn = document.querySelector('.next_div');
var picList = document.querySelectorAll('.picList>li');
var line = document.querySelector('.line');
var attrs = [];
var timer = 0;
for(var i = 0; i <picList.length; i++){
// 先给li在数组中对应的空间内添加一个对象,用于存储它的各项属性数据
attrs[i] = {};
attrs[i].width = css(picList[i],"width");
attrs[i].left = css(picList[i],"left");
attrs[i].top = css(picList[i],"top");
attrs[i].opacity = css(picList[i],"opacity");
attrs[i].zIndex = css(picList[i],"zIndex");
}
prevBtn.onclick = function(){
attrs.push(attrs.shift());
setStyle();
};
nextBtn.onclick = function(){
attrs.unshift(attrs.pop());
setStyle();
};
function setStyle(){
css(line,"opacity",0);
for(var i = 0; i <picList.length; i++){
// css(picList[i],"left",attrs[i].left);
// css(picList[i],"top",attrs[i].top);
// css(picList[i],"opacity",attrs[i].opacity);
// css(picList[i],"zIndex",attrs[i].zIndex);
// 让图片提前显示出来再变化样式
css(picList[i],"zIndex",attrs[i].zIndex);
startMove({
el: picList[i],
target: attrs[i],
time: 500,
type: "easeOut",
callBack: function(){
startMove({
el:line,
target:{opacity:1},
time: 200,
type: "easeOut",
callBack:function(){
console.log(1);
}
});
}
});
}
}
timer = setInterval(function(){
nextBtn.onclick();
},3000);
wrap.onmouseover = function(){
clearInterval(timer);
}
wrap.onmouseout = function(){
timer = setInterval(function(){
nextBtn.onclick();
},3000);
}
})();
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>韩雪冬网站效果 - 妙味课堂 - www.miaov.com</title>
<!--[if lte IE 6]>
<script src="js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>
<script type="text/javascript">
DD_belatedPNG.fix('span');
</script>
<![endif]-->
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="automatic">

<div class="prev_div"></div>
<a class="prev" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a>

<div class="next_div"></div>
<a class="next" href="###">
<span class="ico1"></span>
<span class="ico"></span>
<span class="txt"></span>
</a>

<div class="line"></div>

<ul class="picList">
<li class="pos_0"><a href="http://www.miaov.com"><img src="data:images/8.jpg" width="100" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_1"><a href="http://www.miaov.com"><img src="data:images/1.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_2"><a href="http://www.miaov.com"><img src="data:images/2.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_3"><a href="http://www.miaov.com"><img src="data:images/3.jpg" width="680" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_4"><a href="http://www.miaov.com"><img src="data:images/4.jpg" width="510" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_5"><a href="http://www.miaov.com"><img src="data:images/5.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="data:images/6.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
<li class="pos_6"><a href="http://www.miaov.com"><img src="data:images/7.jpg" width="270" alt="妙味课堂 www.miaov.com" longdesc="http://www.miaov.com" /></a></li>
</ul>
</div>
<p id="footer"><a href="http://www.miaov.com">妙味课堂 - www.miaov.com</a></p>
</body>
</html>
<script type="text/javascript" src="../../startmove.js"></script>
<script type="text/javascript">
/*初始宽度*/
// 使用函数自执行写法,防止全局污染
(function(){
var picList = document.querySelectorAll('.picList>li');
for(var i = 0; i <picList.length;i++){
// 为什么要加个[0]?
var img = picList[i].getElementsByTagName("img")[0];
css(picList[i],"width",img.width);
img.style.width = "100%";
}
})();
/* 左侧按钮的鼠标移入移出动画 */
(function(){
// 由于prev内部的元素层级都比它自身高,盖在它上面,所以如果在prev上加onmouseover或者onmouseout,实际上鼠标根本移动不到prev上,所以添加一个层级较高的prevBtn来防止干扰,nextBtn同理
var prevBtn = document.querySelector('.prev_div');
var prev = document.querySelector('.prev');
var prevSpan = prev.querySelectorAll('span');
prevBtn.onmouseover = function(){
startMove({
el: prevSpan[1],
target: {
left: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[0],
target: {
opacity:1,
left: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[2],
target: {
opacity:1,
left: 53
},
time: 300,
type: "easeBoth"
});
};
prevBtn.onmouseout = function(){
startMove({
el: prevSpan[1],
target: {
left: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[0],
target: {
opacity:0,
left: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: prevSpan[2],
target: {
opacity:0,
left: 65
},
time: 300,
type: "easeBoth"
});
};
})();
/* 右侧按钮的鼠标移入移出动画 */
(function(){
var nextBtn = document.querySelector('.next_div');
var next = document.querySelector('.next');
var nextSpan = next.querySelectorAll('span');
nextBtn.onmouseover = function(){
startMove({
el: nextSpan[1],
target: {
right: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[0],
target: {
opacity:1,
right: 12
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[2],
target: {
opacity:1,
right: 53
},
time: 300,
type: "easeBoth"
});
};
nextBtn.onmouseout = function(){
startMove({
el: nextSpan[1],
target: {
right: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[0],
target: {
opacity:0,
right: 0
},
time: 300,
type: "easeBoth"
});
startMove({
el: nextSpan[2],
target: {
opacity:0,
right: 65
},
time: 300,
type: "easeBoth"
});
};
})();
/*
点击切换动画
*/
(function(){
var wrap = document.querySelector('#automatic');
var prevBtn = document.querySelector('.prev_div');
var nextBtn = document.querySelector('.next_div');
var picList = document.querySelectorAll('.picList>li');
var line = document.querySelector('.line');
var attrs = [];
var timer = 0;
for(var i = 0; i <picList.length; i++){
// 先给li在数组中对应的空间内添加一个对象,用于存储它的各项属性数据
attrs[i] = {};
attrs[i].width = css(picList[i],"width");
attrs[i].left = css(picList[i],"left");
attrs[i].top = css(picList[i],"top");
attrs[i].opacity = css(picList[i],"opacity");
attrs[i].zIndex = css(picList[i],"zIndex");
}
prevBtn.onclick = function(){
attrs.push(attrs.shift());
setStyle();
};
nextBtn.onclick = function(){
attrs.unshift(attrs.pop());
setStyle();
};
function setStyle(){
css(line,"opacity",0);
for(var i = 0; i <picList.length; i++){
// css(picList[i],"left",attrs[i].left);
// css(picList[i],"top",attrs[i].top);
// css(picList[i],"opacity",attrs[i].opacity);
// css(picList[i],"zIndex",attrs[i].zIndex);
// 让图片提前显示出来再变化样式
css(picList[i],"zIndex",attrs[i].zIndex);
startMove({
el: picList[i],
target: attrs[i],
time: 500,
type: "easeOut",
callBack: function(){
startMove({
el:line,
target:{opacity:1},
time: 200,
type: "easeOut",
callBack:function(){
console.log(1);
}
});
}
});
}
}
timer = setInterval(function(){
nextBtn.onclick();
},3000);
wrap.onmouseover = function(){
clearInterval(timer);
}
wrap.onmouseout = function(){
timer = setInterval(function(){
nextBtn.onclick();
},3000);
}
})();
</script>

//动画形式公式
var Tween = {
linear: function (t, b, c, d){
return c*t/d + b;
},
easeIn: function(t, b, c, d){
return c*(t/=d)*t + b;
},
easeOut: function(t, b, c, d){
return -c *(t/=d)*(t-2) + b;
},
easeBoth: function(t, b, c, d){
if ((t/=d/2) < 1) {
return c/2*t*t + b;
}
return -c/2 * ((--t)*(t-2) - 1) + b;
},
easeInStrong: function(t, b, c, d){
return c*(t/=d)*t*t*t + b;
},
easeOutStrong: function(t, b, c, d){
return -c * ((t=t/d-1)*t*t*t - 1) + b;
},
easeBothStrong: function(t, b, c, d){
if ((t/=d/2) < 1) {
return c/2*t*t*t*t + b;
}
return -c/2 * ((t-=2)*t*t*t - 2) + b;
},
elasticIn: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p/4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
},
elasticOut: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d) == 1 ) {
return b+c;
}
if (!p) {
p=d*0.3;
}
if (!a || a < Math.abs(c)) {
a = c;
var s = p / 4;
} else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
},
elasticBoth: function(t, b, c, d, a, p){
if (t === 0) {
return b;
}
if ( (t /= d/2) == 2 ) {
return b+c;
}
if (!p) {
p = d*(0.3*1.5);
}
if ( !a || a < Math.abs(c) ) {
a = c;
var s = p/4;
}
else {
var s = p/(2*Math.PI) * Math.asin (c/a);
}
if (t < 1) {
return - 0.5*(a*Math.pow(2,10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
}
return a*Math.pow(2,-10*(t-=1)) *
Math.sin( (t*d-s)*(2*Math.PI)/p )*0.5 + c + b;
},
backIn: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 1.70158;
}
return c*(t/=d)*t*((s+1)*t - s) + b;
},
backOut: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 2.70158; //回缩的距离
}
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
},
backBoth: function(t, b, c, d, s){
if (typeof s == 'undefined') {
s = 1.70158;
}
if ((t /= d/2 ) < 1) {
return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
}
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
},
bounceIn: function(t, b, c, d){
return c - Tween['bounceOut'](d-t, 0, c, d) + b;
},
bounceOut: function(t, b, c, d){
if ((t/=d) < (1/2.75)) {
return c*(7.5625*t*t) + b;
} else if (t < (2/2.75)) {
return c*(7.5625*(t-=(1.5/2.75))*t + 0.75) + b;
} else if (t < (2.5/2.75)) {
return c*(7.5625*(t-=(2.25/2.75))*t + 0.9375) + b;
}
return c*(7.5625*(t-=(2.625/2.75))*t + 0.984375) + b;
},
bounceBoth: function(t, b, c, d){
if (t < d/2) {
return Tween['bounceIn'](t*2, 0, c, d) * 0.5 + b;
}
return Tween['bounceOut'](t*2-d, 0, c, d) * 0.5 + c*0.5 + b;
}
}
// 获取或者设置样式
function css(el,attr,val){
if(arguments.length == 2){
if(el.currentStyle){
val = el.currentStyle[attr];
} else {
val = getComputedStyle(el)[attr];
}
return parseFloat(val);
} else {
if(attr == "opacity"){
el.style.opacity = val;
el.style.filter = "alpha(opacity= "+val*100+")";
} else {
el.style[attr] = val + "px";
}
}
}
//执行动画
function startMove(init){
clearInterval(init.el.timer);
var t = 0;
var b = {};
var c = {};
var d = init.time/20;
for( var s in init.target){
b[s] = css(init.el,s);
c[s] = init.target[s] - b[s];
}
init.el.timer = setInterval(function(){
t++;
if(t > d){
clearInterval(init.el.timer);
init.callBack&&init.callBack();
} else {
for(var s in init.target){
var val = Tween[init.type](t,b[s],c[s],d);
css(init.el,s,val);
}
}
},20);
}