Using HTML + CSS +js to achieve a simple image switching effects

  • 2020-03-30 03:04:55
  • OfStack

It looks like this.

< img Alt = "" border = 0 SRC =" / / ">
The image switching effects are easy to implement, and the compatibility is good.

The HTML page is as follows
<div class="wrapper"> 
<div id="focus"> 
<li><a href="" target="_blank"><img src="img/01.jpg" alt="QQ Store focus map effect download " /></a></li> 
<li><a href="" target="_blank"><img src="img/02.jpg" alt="QQ Mall focus map effect tutorial " /></a></li> 
<li><a href="" target="_blank"><img src="img/03.jpg" alt="jquery Mall focus map effect " /></a></li> 
<li><a href="" target="_blank"><img src="img/04.jpg" alt="jquery Mall focus diagram code " /></a></li> 
<li><a href="" target="_blank"><img src="img/05.jpg" alt="jquery Mall focus map source " /></a></li> 

</div><!-- wrapper end --> 

CSS styles
<style type="text/css"> 
* {margin:0; padding:0;} 
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;} 
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;} 
.clearfix {zoom:1;} 
ul,li {list-style:none;} 
img {border:0;} 

.wrapper {width:800px; margin:0 auto; padding-bottom:50px;} 

#focus {width:800px; height:280px; overflow:hidden; position:relative;} 
#focus ul {height:380px; position:absolute;} 
#focus ul li {float:left; width:800px; height:280px; overflow:hidden; position:relative; background:#000;} 
#focus ul li div {position:absolute; overflow:hidden;} 
#focus .btnBg {position:absolute; width:800px; height:20px; left:0; bottom:0; background:#000;} 
#focus .btn {position:absolute; width:780px; height:10px; padding:5px 10px; right:0; bottom:0; text-align:right;} 
#focus .btn span {display:inline-block; _display:inline; _zoom:1; width:25px; height:10px; _font-size:0; margin-left:5px; cursor:pointer; background:#fff;} 
#focus .btn span.on {background:#fff;} 
#focus .preNext {width:45px; height:100px; position:absolute; top:90px; background:url(img/sprite.png) no-repeat 0 0; cursor:pointer;} 
#focus .pre {left:0;} 
#focus .next {right:0; background-position:right top;} 

Js script
$(function() { 
var sWidth = $("#focus").width(); //Gets the width of the focus map (display area)
var len = $("#focus ul li").length; //Gets the number of focus graphs
var index = 0; 
var picTimer; 

//The following code adds a number button and a translucent bar after the button, as well as two buttons on the previous and next pages
var btn = "<div class='btnBg'></div><div class='btn'>"; 
for(var i=0; i < len; i++) { 
btn += "<span></span>"; 
btn += "</div><div class='preNext pre'></div><div class='preNext next'></div>"; 
$("#focus .btnBg").css("opacity",0.5); 

//Add a mouse slide event to the small button to display the corresponding content
$("#focus .btn span").css("opacity",0.4).mouseenter(function() { 
index = $("#focus .btn span").index(this); 

//Previous page, next page button transparency processing
$("#focus .preNext").css("opacity",0.2).hover(function() { 
},function() { 

//The previous button
$("#focus .pre").click(function() { 
index -= 1; 
if(index == -1) {index = len - 1;} 

//Next button
$("#focus .next").click(function() { 
index += 1; 
if(index == len) {index = 0;} 

//This example rolls left and right, meaning that all li elements float left in the same row, so you need to calculate the width of the outer ul element
$("#focus ul").css("width",sWidth * (len)); 

//Stop auto play when the mouse is on the focus picture, and start auto play when the mouse is off
$("#focus").hover(function() { 
},function() { 
picTimer = setInterval(function() { 
if(index == len) {index = 0;} 
},4000); //This 4000 represents the interval of autoplay in milliseconds

//The display picture function displays the contents according to the received index value
function showPics(index) { //Ordinary switch
var nowLeft = -index*sWidth; //Calculate the left value of the ul element based on the index value
$("#focus ul").stop(true,false).animate({"left":nowLeft},300); //Animate () adjusts the ul element to scroll to the computed position
//$("#focus .btn span").removeClass("on").eq(index).addClass("on"); //Toggles to the selected effect for the current button
$("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300); //Toggles to the selected effect for the current button

Use left and right to switch pictures

< img Alt = "" border = 0 SRC =" / / ">

Related articles: