js实现图片切换效果_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:20:44
Original
1165 people have browsed it

用js实现点击按钮,图片切换的效果:

 1  <div class="box" id="box"> 2         <div class="img_box" id="img_box"> 3             <img  src="../raw/b1.jpg" class="image"  alt="js实现图片切换效果_html/css_WEB-ITnose" > 4             <img  src="../raw/b2.jpg" class="image"  alt="js实现图片切换效果_html/css_WEB-ITnose" > 5             <img  src="../raw/b3.jpg" class="image"  alt="js实现图片切换效果_html/css_WEB-ITnose" > 6             <img  src="../raw/b4.jpg" class="image"  alt="js实现图片切换效果_html/css_WEB-ITnose" > 7         </div> 8         <div id="left" class="switch"></div> 9         <div id="right" class="switch"></div>10     </div>
Copy after login

结构:用一个固定宽高的div来做最外层的容器,设置overflow为hidden,

然后内层img_box设置宽度为四倍box的宽度,高度相同,也就是说img_box里面盛放四张img,但是可见的只有一张,下面的两个div,left和right是充当按钮实现点击切换图片,切换图片也就是改变img_box的left属性,所以img_box应该设置position为absolute,为了方便起见,box的position设置为relation,这样img_box就是相对box进行定位了。四张图片设置float为left,宽度和高度与box相同.

CSS代码:

 1 *{ 2     margin: 0; 3     padding: 0; 4 } 5 .box{ 6     width: 800px; 7     height: 400px; 8     margin: 20px auto; 9     position: relative;10     overflow: hidden;11 }12 .img_box{13     height: 400px;14     width: 3200px;15     position: absolute;16     -moz-transition: 0.5s;17     -webkit-transition: 0.5s;18 }19 img{20     width: 800px;21     height: 400px;22     float: left;23 }24 .switch{25     width: 200px;26     height: 100%;27     position: absolute;28 29 }30 #left{31     left: 0px;32     top: 0px;33     background: -moz-linear-gradient(left, rgba(84, 84, 84, 0.50), rgba(20%,20%,20%,0));34     background: -webkit-linear-gradient(left, rgba(84, 84, 84, 0.50), rgba(20%,20%,20%,0));35 }36 #right{37     right:0px;38     top: 0px;39     background: -moz-linear-gradient(left, rgba(20%,20%,20%,0), rgba(84, 84, 84,0.5));40     background: -webkit-linear-gradient(left, rgba(20%,20%,20%,0), rgba(84, 84, 84,0.5));41 }42 #left:hover{43     background: -moz-linear-gradient(left, rgba(0, 0, 0,0.5), rgba(20%,20%,20%,0));44     background: -webkit-linear-gradient(left, rgba(0, 0, 0,0.5), rgba(20%,20%,20%,0));45 }46 #right:hover{47     background: -moz-linear-gradient(left, rgba(20%,20%,20%,0), rgba(0, 0, 0,0.5));48     background: -webkit-linear-gradient(left, rgba(20%,20%,20%,0), rgba(0, 0, 0,0.5));49 }
Copy after login

left和right用到了背景颜色和透明度渐变的属性,只添加了火狐浏览器和webkit浏览器,另外现在有的IE浏览器是IE和webkit双内核如360安全浏览器

  background: -moz-linear-gradient(left, rgba(84, 84, 84, 0.50), rgba(20%,20%,20%,0));

  background: -webkit-linear-gradient(left, rgba(84, 84, 84, 0.50), rgba(20%,20%,20%,0));

为了实现切换的时候平滑过渡,所以添加了transition属性:

  -moz-transition: 0.5s;

  -webkit-transition: 0.5s;

js代码:

 1 var box; 2 var count=1; 3 window.onload=function(){ 4     box=document.getElementById("img_box"); 5     var left=document.getElementById("left"); 6     var right=document.getElementById("right"); 7     left.addEventListener("click",_left); 8     right.addEventListener("click",_right); 9     document.body.addEventListener("mouseover",demo);10 }11 function _right(){12     var dis=0;13     if(count<4){14         dis=count*800;15     }else{16         dis=0;17         count=0;18     }19     box.style.left="-"+dis+"px";20     count+=1;21 }22 function _left(event){23     var dis=0;24     if(count>1){25         dis=(2-count)*800;26     }else{27         dis=-3*800;28         count=5;29     }30     box.style.left=dis+"px";31     count-=1;32 }
Copy after login

用全局变量count来记录当前显示的第几张图片,当点击切换按钮的时候根据count来计算应该显示第几张照片,然后计算并设置img_box的left属性即可。

效果:http://huizit1.applinzi.com/A-copy/img_switch/img_switch.html

(tips:第一张和第二张图片都是华妃,第二张是截取的剧里面的原图,感觉颜色太暗所以调了一下颜色放在了第一张)

 
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template