用js实现点击按钮,图片切换的效果:
1 <div class="box" id="box"> 2 <div class="img_box" id="img_box"> 3 <img class="image lazy" src="/static/imghwm/default1.png" data-src="../raw/b1.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 4 <img class="image lazy" src="/static/imghwm/default1.png" data-src="../raw/b2.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 5 <img class="image lazy" src="/static/imghwm/default1.png" data-src="../raw/b3.jpg" alt="js实现图片切换效果_html/css_WEB-ITnose" > 6 <img class="image lazy" src="/static/imghwm/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:第一张和第二张图片都是华妃,第二张是截取的剧里面的原图,感觉颜色太暗所以调了一下颜色放在了第一张)

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software