js实现图片切换效果_html/css_WEB-ITnose
用js实现点击按钮,图片切换的效果:
1 <div class="box" id="box"> 2 <div class="img_box" id="img_box"> 3 <img class="image lazy" src="/static/imghw/default1.png" data-src="../raw/b1.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 4 <img class="image lazy" src="/static/imghw/default1.png" data-src="../raw/b2.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 5 <img class="image lazy" src="/static/imghw/default1.png" data-src="../raw/b3.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 6 <img class="image lazy" src="/static/imghw/default1.png" data-src="../raw/b4.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 7 </div> 8 <div id="left" class="switch"></div> 9 <div id="right" class="switch"></div>10 </div>
结构:用一个固定宽高的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 }
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 }
用全局变量count来记录当前显示的第几张图片,当点击切换按钮的时候根据count来计算应该显示第几张照片,然后计算并设置img_box的left属性即可。
效果:http://huizit1.applinzi.com/A-copy/img_switch/img_switch.html
(tips:第一张和第二张图片都是华妃,第二张是截取的剧里面的原图,感觉颜色太暗所以调了一下颜色放在了第一张)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...
