Home Web Front-end HTML Tutorial js实现图片切换效果_html/css_WEB-ITnose

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

Jun 24, 2016 am 11:20 AM

用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>
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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

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.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles