旋转木马图片效果图,轮播图
- 老规矩先上一张示例图:
HTML代码:
<div class="wrap" id="wrap">
<div class="slide" id="slide">
<ul>
<li><a href="#"><img src="images/slidepic1.jpg" alt=""/>a>li>
<li><a href="#"><img src="images/slidepic3.jpg" alt=""/>a>li>
<li><a href="#"><img src="images/slidepic2.jpg" alt=""/>a>li>
<li><a href="#"><img src="images/slidepic4.jpg" alt=""/>a>li>
<li><a href="#"><img src="images/slidepic5.jpg" alt=""/>a>li>
ul>
<div class="arrow" id="arrow">
<a href="javascript:;" class="prev" id="arrLeft">a>
<a href="javascript:;" class="next" id="arrRight">a>
div>
div>
div>
CSS代码:
blockquote, body, button, dd, dl, dt, fieldset, form, h1, h2, h3, h4, h5, h6, hr, input, legend, li, ol, p, pre, td, textarea, th, ul {
margin: 0;
padding: 0
}
body, button, input, select, textarea {
font: 12px/1.5 "Microsoft YaHei", "微软雅黑", SimSun, "宋体", sans-serif;
color: #666;
}
ol, ul {
list-style: none
}
a {
text-decoration: none
}
fieldset, img {
border: 0;
vertical-align: top;
}
a, input, button, select, textarea {
outline: none;
}
a, button {
cursor: pointer;
}
.wrap {
width: 1200px;
margin: 100px auto;
}
.slide {
height: 500px;
position: relative;
}
.slide li {
position: absolute;
left: 200px;
top: 0;
}
.slide li img {
width: 100%;
}
.arrow {
opacity: 0;
}
.prev, .next {
width: 76px;
height: 112px;
position: absolute;
top: 50%;
margin-top: -56px;
background: url(../images/prev.png) no-repeat;
z-index: 99;
}
.next {
right: 0;
background-image: url(../images/next.png);
}
一些定义:
function my$(id) {
return document.getElementById(id);
}
function animate(element,json,fn) {
clearInterval(element.timeId);
element.timeId=setInterval(function () {
var flag=true;
for(var attr in json){
if(attr=="opacity"){
var current= getAttrValue(element,attr)*100;
var target=json[attr]*100;
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
current=current+step;
element.style[attr]=current/100;
}else if(attr=="zIndex"){
element.style[attr]=json[attr];
}else{
var current= parseInt(getAttrValue(element,attr))||0;
var target=json[attr];
var step=(target-current)/10;
step=step>0?Math.ceil(step):Math.floor(step);
current=current+step;
element.style[attr]=current+"px";
}
if(current!=target){
flag=false;
}
}
if(flag){
clearInterval(element.timeId);
if(fn){
fn();
}
}
},10);
}
Arr Config
- 这里主要在于定义五张图的样式效果
- 定义五张图的层级,透明度,大小,位置,从而达到平铺呈现的样式
- 通过对象进行json格式封装,组成数组,不同的数组下标代表着不同的样式
- 到时候我们会遍历这个数组
var config = [
{
width: 400,
top: 20,
left: 50,
opacity: 0.2,
zIndex: 2
},
{
width: 600,
top: 70,
left: 0,
opacity: 0.8,
zIndex: 3
},
{
width: 800,
top: 100,
left: 200,
opacity: 1,
zIndex: 4
},
{
width: 600,
top: 70,
left: 600,
opacity: 0.8,
zIndex: 3
},
{
width: 400,
top: 20,
left: 750,
opacity: 0.2,
zIndex: 2
}
];
<script>
window.οnlοad=function () {
var flag=true;
var list=my$("slide") .getElementsByTagName("li");
function assign(){
for (var i=0;i
animate(list[i],config[i],function () {
flag=true;
})
}
};
assign();
my$("arrRight").οnclick=function(){
if (flag){
flag=false;
config.push(config.shift());
assign();
}
};
my$("arrLeft").οnclick=function(){
if (flag){
flag=false;
config.unshift(config.pop());
assign();
}
};
my$("slide").οnmοuseοver=function () {
animate(my$("arrow"),{"opacity":1})
};
my$("slide").οnmοuseοut=function () {
animate(my$("arrow"),{"opacity":0})
};
};
script>
逻辑
- 给左右2个角标注册,鼠标进入/退出事件,分别注册渐渐动画透明度的显示和隐藏
- 这个实现效果是通过左右2个小角标的点击事件(onclick)来触发数组的删除和追加
- 即:config.push(config.shift());
- config.unshift(config.pop());
- 在删除追加以后我们需要重新分配一下config 就是我前面定义的assign 这个方法
- flag这个变量的值用Boolean类型来控制动画的执行次数,在上一次动画未执行完毕是不会执行下一次的动画的,这样就防止动画加快,和混乱变小
- 我们在开始flag=为true ;假设所有效果都执行完毕将要开始下一次效果,在执行完一次动画animate之后吧flag赋值为false;最后在callback 回调函数重新吧flag赋值为true;