ºÜ¶àÍøÕ¾É϶¼ÓÐÂÖ²¥Í¼£¬µ«È´ºÜÄÑÕÒµ½Ò»¸öϵͳ½²½âµÄ£¬Òò´ËÕâÀï×öÒ»¸ö¼òµ¥µÄ½éÉÜ£¬Ï£Íû´ó¼Ò¶¼ÄÜÓÐËùÊÕ»ñ£¬Èç¹ûÓÐÄÄЩ²»ÕýÈ·µÄµØ·½£¬Ï£Íû´ó¼Ò¿ÉÒÔÖ¸³ö¡£
ÔÀí£º
½«Ò»Ð©Í¼Æ¬ÔÚÒ»ÐÐÖÐÆ½ÆÌ£¬È»ºó¼ÆËãÆ«ÒÆÁ¿ÔÙÀûÓö¨Ê±Æ÷ʵÏÖ¶¨Ê±ÂÖ²¥¡£
²½ÖèÒ»£º½¨Á¢html»ù±¾²¼¾Ö
ÈçÏÂËùʾ£º
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ÂÖ²¥Í¼</title> </head> <body> <div class="container"> <div class="wrap" style="left:-600px;"> <img src="./img/5.jpg" alt=""> <img src="./img/1.jpg" alt=""> <img src="./img/2.jpg" alt=""> <img src="./img/3.jpg" alt=""> <img src="./img/4.jpg" alt=""> <img src="./img/5.jpg" alt=""> <img src="./img/1.jpg" alt=""> </div> <div class="buttons"> <span>1</span> <span>2</span> <span>3</span> <span>4</span> <span>5</span> </div> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="arrow_left"><</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="arrow_right">></a> </div> </body> </html>
Ö»ÓÐÎåÕÅͼƬ£¬È´Ê¹ÓÃ7ÕÅÀ´ÂÖ²¥£¬ÕâÊÇΪÁËʵÏÖÎÞ·ìÂÖ²¥£¬ºóÃæ»áÏêϸ½éÉÜ¡£
¶ø5¸öspan£¬¼´ÎÒÃÇ¿ÉÒÔʵʱ¿´µ½ÂÖ²¥Í¼Ä¿Ç°Ëù´¦µÄλÖá£
×îºóÊÇÁ½¸ö°´Å¥£¬¿ÉÒÔͨ¹ýËüÀ´¿ØÖÆÇ°½øÓëºóÍË¡£
ÕâÀïÎÒÃÇÐèÒª¶ÔwrapʹÓþø¶Ô¶¨Î»£¬ËùÒÔÓÃleft:-600px;½«µÚÒ»ÕÅͼƬÏÔʾ³öÀ´¡£
²½Öè¶þ£º css²¼¾Ö
Ê×ÏÈ£¬resetcss£¬ÈçÏÂËùʾ£º
* { margin:0; padding:0; } a{ text-decoration: none; }
½Ó×Å£¬ÎÒÃÇΪÁËÈÃͼƬֻÔÚcontainerÖУ¬ËùÒÔÐèÒªÏÞ¶¨Æä¿í¶ÈºÍ¸ß¶È²¢ÇÒʹÓÃoverflow:hidden£»½«ÆäÓàµÄͼƬÒþ²ØÆðÀ´£¬²¢ÇÒÎÒÃÇÏ£ÍûwrapÏà¶ÔÓÚcontainer×óÓÒÒÆ¶¯£¬ËùÒÔÉèÖÃΪrelative£¬ÈçÏ£º
.container { position: relative; width: 600px; height: 400px; margin:100px auto 0 auto; box-shadow: 0 0 5px green; overflow: hidden; }
ÎÒÃÇÉèÖÃwrapÊǾø¶Ô¶¨Î»µÄ£¬ËùÒÔ,ÎÒÃǾͿÉÒÔͨ¹ý¿ØÖÆLeftºÍRightÀ´¿ØÖÆÍ¼Æ¬µÄÒÆ¶¯ÁË¡£ÉèÖÃz-index:1;ÒÔ¶ÔºóÃæ½«Òª·ÅÖõÄbuttons×÷Ϊ²Î¿¼¡£ ÒòΪ¹²ÓÐÆßÕÅͼƬ£¬ËùÒÔwidthΪ4200px£¨Ã¿ÕÅͼƬÎÒÃÇÉèÖÃΪ600X400£©,ÎÒÃÇÖ»ÐèÈÃͼƬ×󸡶¯¼´¿ÉʵÏÖÕ¼ÂúÒ»ÅÅÁË¡£
.wrap { position: absolute; width: 4200px; height: 400px; z-index: 1; }
È»ºóÎÒÃǰÑͼƬÉèÖÃλ×󸡶¯£¬²¢ÏÞ¶¨Æä´óС£¬ÈçÏÂËùʾ£º
.container .wrap img { float: left; width: 600px; height: 400px; }
ÏÖÔÚµÄЧ¹ûÈçÏ£º
¼´ÕâʱÒѾÏÔʾ³öÁ˵ÚÒ»ÕÅͼƬ¡£²¢ÇÒ³äÂúÁËÕû¸öcontainer(containerÊÇÓÐbox-shadowµÄ);
È»ºóÎÒÃǰÑÏÔʾ´ÎÐòµÄbuttons·ÅÔÚͼƬµÄÓÒϽǡ£²¢ÇÒÉèÖÃz-index:2;ÒÔ±£Ö¤buttonsÊÇÔÚͼƬµÄÉÏÃæµÄ¡£
.container .buttons { position: absolute; right: 150px; bottom:20px; width: 100px; height: 10px; z-index: 2; }
È»ºó½«buttonsÏÂÃæµÄspan×öÒ»¸ö¼òµ¥µÄÐÞÊΣ¬²¢ÇÒ¸øºÍͼƬ¶ÔÓ¦µÄspanÉèÖÃÒ»¸öonÀ࣬ÈçÏ£º
.container .buttons span { margin-left: 5px; display: inline-block; width: 20px; height: 20px; border-radius: 50%; background-color: green; text-align: center; color:white; cursor: pointer; } .container .buttons span.on{ background-color: red; }
½ÓÏÂÀ´£¬ÎÒÃǰÑ×óÓÒÇл»µÄ¼ýÍ·¼ÓÉÏ£¬È»ºó×ö¼òµ¥µÄÐÞÊΣ¬×¢Ò⣺ÒòΪÕâÀïʹÓÃʵÌåÀ´±íʾ×óÓÒ¼ýÍ·£¬ËùÒÔÉèÖÃfont-size²ÅÄܸıäÆä´óС£¬
.container .arrow { position: absolute; top: 35%; color: green; padding:0px 14px; border-radius: 50%; font-size: 50px; z-index: 2; display: none; } .container .arrow_left { left: 10px; } .container .arrow_right { right: 10px; } .container:hover .arrow { display: block; } .container .arrow:hover { background-color: rgba(0,0,0,0.2); }
²½ÖèÈý£ºÌí¼ÓjsÂß¼
ÎÒÃÇÊ×ÏÈ»ñÈ¡µ½ wrap£¨ÒòΪҪÉèÖÃÆäleft²ÅÄÜ¿ØÖÆÂÖ²¥Í¼£©£¬È»ºó»ñÈ¡µ½×óÓÒÁ½¸ö¼ýÍ·£¬²¢ÊµÏÖÊÖ¶¯ÂÖ²¥£¬ÈçÏ£º
var wrap = document.querySelector(".wrap"); var next = document.querySelector(".arrow_right"); var prev = document.querySelector(".arrow_left"); next.onclick = function () { next_pic(); } prev.onclick = function () { prev_pic(); } function next_pic () { var newLeft = parseInt(wrap.style.left)-600; wrap.style.left = newLeft + "px"; } function prev_pic () { var newLeft = parseInt(wrap.style.left)+600; wrap.style.left = newLeft + "px"; }
ÖµµÃ×¢ÒâµÄÊÇ£¬ÕâÀïwrap.style.leftÊÇÒ»¸ö×Ö·û´®£¬ËùÒÔҪת»¯ÎªÊý×Ö²ÅÄܽøÐмÆË㣬¶øÉ趨leftʱ¾ÍÒª¼ÓÉÏpx³ÉΪһ¸ö×Ö·û´®ÁË¡£
ÏÖÔÚÎÒÃÇÀ´²âÊÔһϣº
¿ÉÒÔ¿´µ½£¬ÔÚµÚһҳʱ£¬leftֵΪ-600£¬ËùÒÔÎÒÃÇ¿ÉÒÔµã»÷Ò»´ÎÉÏÒ»ÕÅ£¬µ«Êǵ±ÔÙµã»÷Ò»´Îʱ£¬¾Í³öÏÖÁ˿հס£Í¬ÑùµÄ£¬ÏÂÒ»Õŵã»÷£¬µ½-3600ÊÇ×îºóÒ»ÕÅ£¬¾Í²»ÄÜÔÙ¼ÌÐøµã»÷ÁË¡£¡¡¡¡
Ò²¾ÍÊÇ˵£¬µ±ÎÒÃǵã»÷ÏÂÒ»Õŵ½-3600px£¨ÕâÊǵÚÒ»ÕÅͼƬ£©Ê±£¬ÎÒÃÇÐèҪϴÎÌø×ªµ½µÚ¶þÕÅ£¬¼´-1200px£»ÕâÑù²ÅÄÜÕý³£Ìø×ª£»
ͬÀí£¬µ±ÎÒÃǵã»÷ÉÏÒ»Õŵ½0px£¨ÕâÊǵÚÎåÕÅͼƬʱ£©£¬ÎÒÃÇÏ£ÍûÏ´ÎÌø×ªµ½µÚËÄÕÅ£¬¼´-2400px£»
°´ÕÕÕâÑùµÄ˼·ÎÒÃÇÖØÐ½«next_picºÍprev_picº¯ÊýÐÞ¸ÄÈçÏ£º
function next_pic () { var newLeft; if(wrap.style.left === "-3600px"){ newLeft = -1200; }else{ newLeft = parseInt(wrap.style.left)-600; } wrap.style.left = newLeft + "px"; } function prev_pic () { var newLeft; if(wrap.style.left === "0px"){ newLeft = -2400; }else{ newLeft = parseInt(wrap.style.left)+600; } wrap.style.left = newLeft + "px"; }
Õâʱ£¬ÎÒÃǾͿÉÒÔ·¢ÏÖͼƬ¿ÉÒÔÑ»·²¥·ÅÁË£¡
µ«ÊÇ£¬ÕâʱÎÒÃǽö½öʱÊÖ¶¯Ñ»·²¥·ÅµÄ£¬ÎÒÃÇÈç¹ûÏ£Íû×Ô¶¯²¥·Å£¬Ê¹ÓÃsetInterval()¼´¿É£¬ÈçÏÂËùʾ£º
var timer = null; function autoPlay () { timer = setInterval(function () { next_pic(); },1000); } autoPlay();
¼´ÏÈÉ趨һ¸ö¼ÆÊ±Æ÷£¬È»ºó´´½¨Ò»¸ö¿ÉÒÔ×Ô¶¯²¥·ÅµÄº¯Êý£¬×îºóµ÷ÓÃÕâ¸öº¯Êý¼´¿É¡£ ÏÖÔÚËü¾Í¿ÉÒÔ×Ô¶¯²¥·ÅÁË£¬Ð§¹ûÈçÏ£º
¡¡¡¡
µ«ÊÇÈç¹ûÎÒÃÇÏëÒª×Ðϸ¿´ÆäÖÐÒ»¸öͼƬµÄʱºò£¬ÎÒÃÇÏ£ÍûÂÖ²¥Í¼Í£Ö¹²¥·Å£¬Ö»ÒªclearInterval()¼´¿É£¬ÈçÏ£º
var container = document.querySelector(".container"); container.onmouseenter = function () { clearInterval(timer); } container.onmouseleave = function () { autoPlay(); }
ÏÖÔÚ£¬Ö»ÒªÎÒÃǰÑÊó±êÒÆÈëÂÖ²¥Í¼ÖУ¬ÕâʱÂÖ²¥Í¼¾Í²»»á²¥·ÅÁË¡£¶øÒÆ¿ªÊó±êÖ®ºó£¬ÂÖ²¥Í¼×Ô¶¯²¥·Å¡£
µ«Êǵ½Ä¿Ç°ÎªÖ¹£¬ÂÖ²¥Í¼Ï·½µÄСԲµã»¹Ã»Óж¯£¬ÏÖÔÚÎÒÃǾÍʵÏÖËü¡£
ÔÀíºÜ¼òµ¥£¬¼´ÉèÖÃbuttonsµÄindex³õʼֵΪ0£¬¼´µÚÒ»¸öspanµÄclassΪon£¬È»ºó´¥·¢next_picº¯Êýʱ£¬index¼Ó1£¬µ±´¥·¢prev_picº¯Êýʱ£¬inex¼õ1£¬ ²¢ÉèÖõ±Ç°indexµÄСԲµãµÄclassΪon£¬ Õâ¾ÍÒªÇóindex±ØÐëÉèÖÃΪȫ¾Ö±äÁ¿£¬²ÅÄܱ£Ö¤ËüÔÚÿһ¸öº¯ÊýµÄ×÷ÓÃÓòÖС£
Ìí¼ÓshowCurrentDotº¯Êý£º
var index = 0; var dots = document.getElementsByTagName("span"); function showCurrentDot () { for(var i = 0, len = dots.length; i < len; i++){ dots[i].className = ""; } dots[index].className = "on"; }
ÔÚnext_picÖÐÌí¼Ó´úÂ룺
index++; if(index > 4){ index = 0; }
ÔÚprev_picÖÐÌí¼Ó´óÂð£º
index--; if(index < 0){ index = 4; } showCurrentDot();
ÕâÑù£¬ÂÖ²¥Í¼¾Í¿ÉÒÔ×Ô¶¯Çл»£¬²¢ÇÒСԲµãÒ²ÔÚËæ×ÅͼƬµÄ±ä»¯¶ø±ä»¯ÁË¡£
µ«ÊÇ£¬Õâ¾àÀëÎÒÃǾ³£¿´µ½µÄÂÖ²¥Í¼»¹ÓÐÒ»¶¨¾àÀë - - - µ±µã»÷СԲµãʱ£¬ ¾Í¿ÉÌø×ªµ½ÏàӦͼƬ¡£ ʵÏÖÔÀí¼´£º µã»÷СԲµã£¬¾ÍʹwrapµÄLeft±ä³ÉÏàÓ¦µÄÖµ¡£´úÂëÈçÏ£º
for (var i = 0, len = dots.length; i < len; i++){ (function(i){ dots[i].onclick = function () { var dis = index - i; if(index == 4 && parseInt(wrap.style.left)!==-3000){ dis = dis - 5; } //ºÍʹÓÃprevºÍnextÏàͬ£¬ÔÚ×ʼµÄÕÕÆ¬5ºÍ×îÖÕµÄÕÕÆ¬1ÔÚʹÓÃʱ»á³öÏÖÎÊÌ⣬µ¼Ö·ûºÅºÍλÊýµÄ³ö´í£¬×öÏàÓ¦µØ´¦Àí¼´¿É if(index == 0 && parseInt(wrap.style.left)!== -600){ dis = 5 + dis; } wrap.style.left = (parseInt(wrap.style.left) + dis * 600)+"px"; index = i; showCurrentDot(); } })(i); }
ÔÀí¾ÍÊǵ±µã»÷µ½Ð¡Ô²µãʱ£¬µÃµ½ÏàÓ¦µÄiÖµ£¬Õâ¸öiÖµÒ²¾ÍÊÇspanµÄindexÖµ£¬ÎÒÃÇÄÃËûºÍÈ«¾Ö±äÁ¿index×÷±È½Ï£¬È»ºóÖØÐÂÉèÖÃwrap.style.leftµÄÖµ£¬È»ºó°ÑiÖµ¸´ÖƸøÈ«¾Ö±äÁ¿index£¬×îºóÏÔʾµ±Ç°µÄСԵ㼴¿É¡£ÖµµÃ×¢ÒâµÄÊÇ£ºÕâÀïÉæ¼°µ½Á˱հüµÄ¸ÅÄÈç¹ûÖ±½ÓʹÓÃforÑ»·£¬Ôò²»Äܵõ½ÕýÈ·µÄ½á¹û¡£
×îÖÕЧ¹ûÈçͼ£º
¡¡
×îÖÕ´úÂëÈçÏÂËùʾ£º
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>ÂÖ²¥Í¼</title> <style> * { margin:0; padding:0; } a{ text-decoration: none; } .container { position: relative; width: 600px; height: 400px; margin:100px auto 0 auto; box-shadow: 0 0 5px green; overflow: hidden; } .container .wrap { position: absolute; width: 4200px; height: 400px; z-index: 1; } .container .wrap img { float: left; width: 600px; height: 400px; } .container .buttons { position: absolute; right: 5px; bottom:40px; width: 150px; height: 10px; z-index: 2; } .container .buttons span { margin-left: 5px; display: inline-block; width: 20px; height: 20px; border-radius: 50%; background-color: green; text-align: center; color:white; cursor: pointer; } .container .buttons span.on{ background-color: red; } .container .arrow { position: absolute; top: 35%; color: green; padding:0px 14px; border-radius: 50%; font-size: 50px; z-index: 2; display: none; } .container .arrow_left { left: 10px; } .container .arrow_right { right: 10px; } .container:hover .arrow { display: block; } .container .arrow:hover { background-color: rgba(0,0,0,0.2); } </style> </head> <body> <div class="container"> <div class="wrap" style="left: -600px;"> <img src="./img/5.jpg" alt=""> <img src="./img/1.jpg" alt=""> <img src="./img/2.jpg" alt=""> <img src="./img/3.jpg" alt=""> <img src="./img/4.jpg" alt=""> <img src="./img/5.jpg" alt=""> <img src="./img/1.jpg" alt=""> </div> <div class="buttons"> <span class="on">1</span> <span>2</span> <span>3</span> <span>4</span> <span>5</span> </div> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="arrow arrow_left"><</a> <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="arrow arrow_right">></a> </div> <script> var wrap = document.querySelector(".wrap"); var next = document.querySelector(".arrow_right"); var prev = document.querySelector(".arrow_left"); next.onclick = function () { next_pic(); } prev.onclick = function () { prev_pic(); } function next_pic () { index++; if(index > 4){ index = 0; } showCurrentDot(); var newLeft; if(wrap.style.left === "-3600px"){ newLeft = -1200; }else{ newLeft = parseInt(wrap.style.left)-600; } wrap.style.left = newLeft + "px"; } function prev_pic () { index--; if(index < 0){ index = 4; } showCurrentDot(); var newLeft; if(wrap.style.left === "0px"){ newLeft = -2400; }else{ newLeft = parseInt(wrap.style.left)+600; } wrap.style.left = newLeft + "px"; } var timer = null; function autoPlay () { timer = setInterval(function () { next_pic(); },2000); } autoPlay(); var container = document.querySelector(".container"); container.onmouseenter = function () { clearInterval(timer); } container.onmouseleave = function () { autoPlay(); } var index = 0; var dots = document.getElementsByTagName("span"); function showCurrentDot () { for(var i = 0, len = dots.length; i < len; i++){ dots[i].className = ""; } dots[index].className = "on"; } for (var i = 0, len = dots.length; i < len; i++){ (function(i){ dots[i].onclick = function () { var dis = index - i; if(index == 4 && parseInt(wrap.style.left)!==-3000){ dis = dis - 5; } //ºÍʹÓÃprevºÍnextÏàͬ£¬ÔÚ×ʼµÄÕÕÆ¬5ºÍ×îÖÕµÄÕÕÆ¬1ÔÚʹÓÃʱ»á³öÏÖÎÊÌ⣬µ¼Ö·ûºÅºÍλÊýµÄ³ö´í£¬×öÏàÓ¦µØ´¦Àí¼´¿É if(index == 0 && parseInt(wrap.style.left)!== -600){ dis = 5 + dis; } wrap.style.left = (parseInt(wrap.style.left) + dis * 600)+"px"; index = i; showCurrentDot(); } })(i); } </script> </body> </html>
×ܽ᣺
ʵÏÖÒ»¸öÂÖ²¥Í¼»¹ÊDz»Äѵ쬴óÌå˼·£º ÏÈ´´½¨Ò»¸ödiv£¬ÏÞ¶¨Æä¿í¶ÈºÍ¸ß¶È£¬overflow£ºhidden¡£È»ºó´´½¨Ò»¸öװͼƬµÄdiv£¬¿í¶ÈΪËùÓÐͼƬµÄ×Ü¿í¶È£¬²¢ÇÒʹÆä¸¡¶¯£¬ÕâÑùËùÓеÄͼƬ¾Í´¦ÓÚÒ»ÐÐÖС£È»ºóΪÁËʵÏÖÎÞ·ì¹ö¶¯£¬ËùÒÔÐèÒªÔÚÊ×β·Ö±ðÌí¼ÓÒ»ÕŹý¶ÉͼƬ¡£ ÏÈÌí¼ÓÁ½¸ö°´Å¥£¬ ʹÆä¿ÉÒÔÊÖ¶¯ÂÖ²¥£¬È»ºóÖ»ÐèÒªÌí¼ÓÒ»¸ö¶¨Ê±Æ÷ʹÆä³¯Ò»¸ö·½Ïò×Ô¶¯ÂÖ²¥¼´¿É£¬ÒòΪÓû§ÓÐʱÐèÒª²é¿´ÏêÇ飬ËùÒÔµ±Êó±ê½øÈëʱ¾Íclear¶¨Ê±Æ÷£¬»¬³öÔÙ¶¨Ê±²¥·Å¡£ÎªÁ˸üºÃµØÓû§ÌåÑ飬ÎÒÃÇÔÙÏÂÃæÌí¼ÓÁËÒ»ÅÅСԲµãÇôúÂ룬Óû§¿ÉÒÔÇå³þµØÖªµÀÏÖÔÚËù´¦µÄλÖ㬠×îºó£¬ ÀûÓñհüʹµÃÓû§¿ÉÒÔÖ±½Óͨ¹ýµã»÷СԲµãÇл»Í¼Æ¬¡£