仿搜狐女人频道FLASH图片切换效果 时间:2020-05-12 人气:0 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312"> <head> <title>JS图片切换 ::</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="generator" content="editplus" /> <meta name="author" content="eLore" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <style type="text/css"> * { margin:0; padding:0; } body { margin:0; color:#88c; background:#333; } img { margin:0; padding:0; border:0; } #js_F { position:relative; top:10px; left:10px; overflow:hidden; width:395px; height:185px; background:#33c; } .div_img { position:absolute; top:0; width:295px; height:185px; cursor:pointer; } #div_img_0 { z-index:5; left:0; } #div_img_1 { z-index:4; left:25px; } #div_img_2 { z-index:3; left:50px; } #div_img_3 { z-index:2; left:75px; } #div_img_4 { z-index:1; left:100px; } </style> </head> <body> <div> <div><a href="#"><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25700.jpg" /></a><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25701.gif" onmouseover="jsf_mmove(0)" /></div> <div><a href="#"><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25702.jpg" /></a><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25701.gif" onmouseover="jsf_mmove(1)" /></div> <div><a href="#"><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25703.jpg" /></a><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25701.gif" onmouseover="jsf_mmove(2)" /></div> <div><a href="#"><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25704.jpg" /></a><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25701.gif" onmouseover="jsf_mmove(3)" /></div> <div><a href="#"><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25705.jpg" /></a><img onerror="javascript:this.src='/skin1/img/error.jpg';" src="https://img.qb5200.com/download-x/20200511/25701.gif" onmouseover="jsf_mmove(4)" /></div> </div> <script type="text/javascript"> <!--///*--><![CDATA[/*><!--*/ var div_imgs = document.getElementById("js_F").getElementsByTagName("div"); var imgLeft = [0, 25, 50, 75, 100]; //5个图片的初始left值 var imgWidth = 270; //图片的宽度 var atf = [true, true, true, true, true]; //5个图片的位置:true为右边,false为左边 var speed1 = 10, speed2 = 2000, mo = 15; //速度 var sTo; function jsf_move(n){ clearTimeout(sTo); var thisL; //图片left值 if (atf[n]) { //要移动的图片在右边 //需移动的图片(包括该图片左边的所有图片)向左边移动) for (var i=0; i<=n; i++) { //当前值-(图片宽度-(当前值-初始值))/速度 thisL = parseInt(getStyle(div_imgs[i],"left")); div_imgs[i].style.left = thisL - Math.ceil((imgWidth-(imgLeft[i]-thisL))/mo) + "px"; if (i<n) atf[i] = false; //更改图片的位置状态 } } else { //要移动的图片在左边 //需移动的图片(包括该图片右边的所有图片)向右边移动) for (var i=4; i>=n; i--) { //当前值+(当前值-初始值)/速度 thisL = parseInt(getStyle(div_imgs[i],"left")); div_imgs[i].style.left = thisL + Math.ceil(Math.abs(thisL-imgLeft[i])/mo) + "px"; if (i>n) atf[i] = true; //更改图片的位置状态 } } thisL = parseInt(getStyle(div_imgs[n],"left")); if ((atf[n] && thisL>(imgLeft[n]-imgWidth)) || (!atf[n] && thisL<imgLeft[n])) { //当前图片移动未结束,继续移动当前图片 sTo = setTimeout(function(){jsf_move(n);}, speed1); } else { //当前图片移动结束,准备移动下一张图片 if (n>=3 && atf[n]) { atf[n] = false; sTo = setTimeout(function(){jsf_move(n);}, speed2); } else if (n==0 && !atf[n]) { atf[n] = true; sTo = setTimeout(function(){jsf_move(n);}, speed2); } else if (atf[n]){ atf[n] = false; sTo = setTimeout(function(){jsf_move(++n);}, speed2); } else { sTo = setTimeout(function(){jsf_move(--n);}, speed2); } } } //判断图片的位置 function jsf_mmove(n){ clearTimeout(sTo); switch (n){ case 0 : atf[0] = false; jsf_move(0); break; case 1 : case 2 : case 3 : if (atf[n-1]) { sTo = setTimeout(function(){jsf_move(n-1);}, speed1); } else if (atf[n]) { atf[n-1] = true; sTo = setTimeout(function(){jsf_move(n-1);}, speed1); } else { sTo = setTimeout(function(){jsf_move(n);}, speed1); } break; case 4 : atf = [false, false, false, true, true]; jsf_move(3); break; } } window.onload = function(){ sTo = setTimeout("jsf_move(0)",speed2); } function getStyle( elem, name ) { if (elem.style[name]) { return elem.style[name]; } else if (elem.currentStyle) { return elem.currentStyle[name]; } else if (document.defaultView && document.defaultView.getComputedStyle) { name = name.replace(/([A-Z])/g,"-$1"); name = name.toLowerCase(); var s = document.defaultView.getComputedStyle(elem,""); return s && s.getPropertyValue(name); } else { return null; } } /*]]>*///--> </script> </body> </html> [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]这个是有一点问题的,就是鼠标在移动的图片上滑过时,图片会停顿一下,解决方法是增加一个图片位置标记:-1为左边;0为移动中;1为右边,也可以增加一个移动方向的参数,具体就懒得写了。下面再给一个自己写的图片切换效果:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312"> <head> <title>JS图片切换 ::</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="generator" content="editplus" /> <meta name="author" content="eLore" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <style type="text/css"> * { margin:0; padding:0; } body { margin:0; color:#88c; background:#333; } img { margin:0; padding:0; border:0; } #js_F { position:relative; top:10px; left:10px; overflow:hidden; width:270px; height:185px; background:#33c; } #js_F img{ position:absolute; top:0; left:0; width:270px; height:185px; } </style> </head> <body> <div></div> <script type="text/javascript"> <!--///*--><![CDATA[/*><!--*/ var js_F = document.getElementById("js_F"); var imgList = ["https://img.qb5200.com/download-x/20200511/25700.jpg", "https://img.qb5200.com/download-x/20200511/25702.jpg", "https://img.qb5200.com/download-x/20200511/25703.jpg", "https://img.qb5200.com/download-x/20200511/25704.jpg", "https://img.qb5200.com/download-x/20200511/25705.jpg", "http://www.lmwq.com.cn/imglist/06.jpg", "http://www.lmwq.com.cn/imglist/07.jpg", "http://www.lmwq.com.cn/imglist/08.jpg", ]; var imgTemp = new Array(); for (i=0; i<imgList.length; i++){ imgTemp[i] = new Image(); imgTemp[i].src = imgList[i]; } var imgs = new Array(); var imgID = 0, nextImgID, proveImgID; var tf = true; //图片移动方向标志 var speed1 = 10; speed2 = 3000; //速度 function imgInit(){ var content = ''; for (i=0; i<imgList.length; i++){ content += '<img onerror="javascript:this.src='/skin1/img/error.jpg';" src="' + imgList[i] + '" style="left:0;" />\n'; } js_F.innerHTML = content; imgs = js_F.getElementsByTagName('img'); imgs[0].style.zIndex = 20; imgs[1].style.zIndex = 15; } function imgChange(){ if ((imgID+1)<imgList.length){ nextImgID = imgID + 1; } else if (imgID<imgList.length) { nextImgID = 0; } else { imgID = 0; nextImgID = imgID + 1; } imgs[imgID].style.zIndex = 20; imgs[nextImgID].style.zIndex = 15; setTimeout('imgShow()',speed2); } function imgShow(){ if (tf){ //imgID向左边移动2/3,nextImgID向右边1/3 if (parseInt(imgs[imgID].style.left)>-180){ imgs[imgID].style.left = (parseInt(imgs[imgID].style.left)-10) + 'px'; imgs[nextImgID].style.left = (parseInt(imgs[nextImgID].style.left)+5) + 'px'; setTimeout('imgShow()',speed1); } else { tf = !tf; imgs[imgID].style.zIndex = 15; imgs[nextImgID].style.zIndex = 20; setTimeout('imgShow()',speed1); } } else { if (parseInt(imgs[imgID].style.left)<0){ imgs[imgID].style.left = (parseInt(imgs[imgID].style.left)+10) + 'px'; imgs[nextImgID].style.left = (parseInt(imgs[nextImgID].style.left)-5) + 'px'; setTimeout('imgShow()',speed1); } else { imgs[imgID].style.zIndex = 10; tf = !tf; imgID++; imgChange(); } } } imgInit(); imgChange(); /*]]>*///--> </script> </body> </html> [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 加载全部内容