鼠标点击划过按钮,图片左右切换轮播效果代码
用JS实现图片轮播效果代码(一)
⽤JS实现图⽚轮播效果代码(⼀)⼀.实现原理(1)将所有图⽚放在⼀个⽗容器div⾥⾯,通过display属性来设置图⽚的出现与隐藏;(2)轮播图分为⼿动轮播和⾃动轮播;⼿动轮播的重点是每次点击图⽚下⽅的⼩圆圈,获得它的索引号,并让与之对应索引号的图⽚显⽰,并且此时的⼩圆圈为⾼亮显⽰;⾃动轮播:利⽤定时器setInterval(),来每隔⼀定的时间来播放⼀次图⽚。
(3)所有的基础知识:dom操作,定时器,事件运⽤。
⼆.轮播图页⾯布局:<div id="content"> <!-- 总的⽗容器 --><div class="carousel-inner"> <!-- 包含图⽚的容器 --><div class="item "><img src="./img/lunbo1.jpg" alt="第⼀张图⽚"></div><div class="item"><img src="./img/lunbo2.jpg" alt="第⼆张图⽚"></div><div class="item"><img src="./img/lunbo3.jpg" alt="第三张图⽚"></div></div><!-- 图⽚下⽅的指⽰圆圈--><ul class="carousel-indicators"><li id='pic1'>●</li><li id='pic2'>●</li><li id='pic3'>●</li></ul><!-- 图⽚左右⽅来回滚动图⽚的左右箭头--><a href="#" class="carousel-control prev"><span><</span></a><a href="#" class="carousel-control next"><span>></span></a></div>三.轮播图的css样式:#content{width: 100%;height:500px;position: relative;}.carousel-inner{position: relative;width: 100%;overflow: hidden;height:500px;}.carousel-inner>.item>img{display: block;line-height: 1;z-index: 1;}.carousel-indicators{position: absolute;bottom:10px;left:45%;z-index: 2;list-style-type: none;}.carousel-indicators li{display:inline-block;padding: 0 15px;font-size: 24px;color:#999;cursor: pointer;z-index: 2;}.active1{font-size: 28px;color:#fff;}.carousel-control{position: absolute;text-decoration:none;color: #999;font-weight: bold;opacity: .5;font-size: 40px;z-index: 3;}.carousel-control:hover{color:fff;text-decoration: none;opacity: .9;outline: none;font-size: 42px;}.prev{top: 30%;left:20px;}.next{top:30%;right: 20px;}四.轮播图的js实现代码:window.onload = function(){//轮播初始化var lunbo = document.getElementById('content');var imgs = lunbo.getElementsByTagName('img');var uls = lunbo.getElementsByTagName('ul');var lis = lunbo.getElementsByTagName('li');//初始状态下,⼀个圆圈为⾼亮模式lis[0].style.fontSize = '26px';lis[0].style.color = '#fff';//定义⼀个全局变量,⽤来进⾏⾃动轮播图⽚顺序的变化var pic_index = 1;//⾃动轮播.使⽤pic_time记录播放,可以随时使⽤clearInterval()清除掉。
javascript实现图片左右滚动效果【可自动滚动,有左右按钮】
javascript实现图⽚左右滚动效果【可⾃动滚动,有左右按钮】本⽂实例讲述了javascript实现图⽚左右滚动效果。
分享给⼤家供⼤家参考,具体如下:html代码:<!doctype html><html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="renderer" content="webkit"/><meta name="keywords" content=""/><meta name="description" content=""/><title>图⽚滚动</title><style>*{margin:0;padding:0;}ul{list-style:none;}img{border:0;}.scroll{width:358px;height:63px;}.scroll_left{width:23px;height:63px;background:url(images/btn_left.jpg) no-repeat;float:left;}.scroll_right{width:23px;height:63px;background:url(images/btn_right.jpg) left no-repeat;float:left;}.pic{width:312px;height:73px;float:left;}.pic ul{display:block;}.pic li{float:left;display:inline;width:104px;text-align:center;}</style></head><body><div style="margin:100px auto;width:358px;"><div class="scroll"><div class="scroll_left" id="LeftArr"></div><div class="pic" id="scrollPic"><ul><li><a href="#" target="_blank" title=""><img src="images/pic01.png" width="100" height="63" alt="" /></a></li><li><a href="#" target="_blank" title=""><img src="images/pic02.jpg" width="100" height="63" alt="" /></a></li><li><a href="#" target="_blank" title=""><img src="images/pic03.jpg" width="100" height="63" alt="" /></a></li><li><a href="#" target="_blank" title=""><img src="images/pic04.jpg" width="100" height="63" alt="" /></a></li><li><a href="#" target="_blank" title=""><img src="images/pic05.jpg" width="100" height="63" alt="" /></a></li><li><a href="#" target="_blank" title=""><img src="images/pic06.jpg" width="100" height="63" alt="" /></a></li></ul></div><div class="scroll_right" id="RightArr"></div></div></div></body></html><script src="scrollPic.js"></script><script>window.onload = function(){scrollPic();}function scrollPic() {var scrollPic = new ScrollPic();scrollPic.scrollContId = "scrollPic"; //内容容器IDscrollPic.arrLeftId = "LeftArr";//左箭头IDscrollPic.arrRightId = "RightArr"; //右箭头IDscrollPic.frameWidth = 312;//显⽰框宽度scrollPic.pageWidth = 104; //翻页宽度scrollPic.speed = 10; //移动速度(单位毫秒,越⼩越快)scrollPic.space = 10; //每次移动像素(单位px,越⼤越快)scrollPic.autoPlay = true; //⾃动播放scrollPic.autoPlayTime = 3; //⾃动播放间隔时间(秒)scrollPic.initialize(); //初始化}</script>scrollPic.js 代码:var sina = {return eval('document.getElementById("' + objName + '")')} else {return eval('document.all.' + objName)}},isIE : navigator.appVersion.indexOf("MSIE") != -1 ? true : false,addEvent : function (l, i, I) {if (l.attachEvent) {l.attachEvent("on" + i, I)} else {l.addEventListener(i, I, false)}},delEvent : function (l, i, I) {if (l.detachEvent) {l.detachEvent("on" + i, I)} else {l.removeEventListener(i, I, false)}},readCookie : function (O) {var o = "",l = O + "=";if (document.cookie.length > 0) {var i = document.cookie.indexOf(l);if (i != -1) {i += l.length;var I = document.cookie.indexOf(";", i);if (I == -1)I = document.cookie.length;o = unescape(document.cookie.substring(i, I))}};return o},writeCookie : function (i, l, o, c) {var O = "",I = "";if (o != null) {O = new Date((new Date).getTime() + o * 3600000);O = "; expires=" + O.toGMTString()};if (c != null) {I = ";domain=" + c};document.cookie = i + "=" + escape(l) + O + I},readStyle : function (I, l) {if (I.style[l]) {return I.style[l]} else if (I.currentStyle) {return I.currentStyle[l]} else if (document.defaultView && document.defaultView.getComputedStyle) { var i = document.defaultView.getComputedStyle(I, null);return i.getPropertyValue(l)} else {return null}}};//滚动图⽚构造函数//UI&UE Dept. mengjia//080623function ScrollPic(scrollContId, arrLeftId, arrRightId, dotListId) {this.scrollContId = scrollContId;this.arrLeftId = arrLeftId;this.arrRightId = arrRightId;this.dotListId = dotListId;this.dotClassName = "dotItem";this.dotOnClassName = "dotItemOn";this.dotObjArr = [];this.pageWidth = 0;this.frameWidth = 0;this.speed = 10;this.space = 10;this.autoPlay = true;this.autoPlayTime = 5;var _autoTimeObj,_scrollTimeObj,_state = "ready";this.stripDiv = document.createElement("DIV");this.listDiv01 = document.createElement("DIV");this.listDiv02 = document.createElement("DIV");if (!ScrollPic.childs) {ScrollPic.childs = []};this.ID = ScrollPic.childs.length;ScrollPic.childs.push(this);this.initialize = function () {if (!this.scrollContId) {throw new Error("必须指定scrollContId.");return};this.scrollContDiv = sina.$(this.scrollContId);if (!this.scrollContDiv) {throw new Error("scrollContId不是正确的对象.(scrollContId = \"" + this.scrollContId + "\")");return};this.scrollContDiv.style.width = this.frameWidth + "px";this.scrollContDiv.style.overflow = "hidden";this.listDiv01.innerHTML = this.listDiv02.innerHTML = this.scrollContDiv.innerHTML;this.scrollContDiv.innerHTML = "";this.scrollContDiv.appendChild(this.stripDiv);this.stripDiv.appendChild(this.listDiv01);this.stripDiv.appendChild(this.listDiv02);this.stripDiv.style.overflow = "hidden";this.stripDiv.style.zoom = "1";this.stripDiv.style.width = "32766px";if(-[1,]){this.listDiv01.style.cssFloat = "left";this.listDiv02.style.cssFloat = "left";}else{this.listDiv01.style.styleFloat = "left";this.listDiv02.style.styleFloat = "left";}sina.addEvent(this.scrollContDiv, "mouseover", Function("ScrollPic.childs[" + this.ID + "].stop()"));sina.addEvent(this.scrollContDiv, "mouseout", Function("ScrollPic.childs[" + this.ID + "].play()"));if (this.arrLeftId) {this.arrLeftObj = sina.$(this.arrLeftId);if (this.arrLeftObj) {sina.addEvent(this.arrLeftObj, "mousedown", Function("ScrollPic.childs[" + this.ID + "].rightMouseDown()")); sina.addEvent(this.arrLeftObj, "mouseup", Function("ScrollPic.childs[" + this.ID + "].rightEnd()"));sina.addEvent(this.arrLeftObj, "mouseout", Function("ScrollPic.childs[" + this.ID + "].rightEnd()"))}};if (this.arrRightId) {this.arrRightObj = sina.$(this.arrRightId);if (this.arrRightObj) {sina.addEvent(this.arrRightObj, "mousedown", Function("ScrollPic.childs[" + this.ID + "].leftMouseDown()")); sina.addEvent(this.arrRightObj, "mouseup", Function("ScrollPic.childs[" + this.ID + "].leftEnd()"));sina.addEvent(this.arrRightObj, "mouseout", Function("ScrollPic.childs[" + this.ID + "].leftEnd()"))}};if (this.dotListId) {this.dotListObj = sina.$(this.dotListId);if (this.dotListObj) {var pages = Math.round(this.listDiv01.offsetWidth / this.frameWidth + 0.4),i,tempObj;for (i = 0; i < pages; i++) {tempObj = document.createElement("span");this.dotListObj.appendChild(tempObj);this.dotObjArr.push(tempObj);if (i == this.pageIndex) {tempObj.className = this.dotClassName} else {tempObj.className = this.dotOnClassName};tempObj.title = "第" + (i + 1) + "页";sina.addEvent(tempObj, "click", Function("ScrollPic.childs[" + this.ID + "].pageTo(" + i + ")"))}};if (this.autoPlay) {this.play()}};this.leftMouseDown = function () {if (_state != "ready") {return};_state = "floating";_scrollTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].moveLeft()", this.speed)};this.rightMouseDown = function () {if (_state != "ready") {return};_state = "floating";_scrollTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].moveRight()", this.speed)};this.moveLeft = function () {if (this.scrollContDiv.scrollLeft + this.space >= this.listDiv01.scrollWidth) {this.scrollContDiv.scrollLeft = this.scrollContDiv.scrollLeft + this.space - this.listDiv01.scrollWidth } else {this.scrollContDiv.scrollLeft += this.space};this.accountPageIndex()};this.moveRight = function () {if (this.scrollContDiv.scrollLeft - this.space <= 0) {this.scrollContDiv.scrollLeft = this.listDiv01.scrollWidth + this.scrollContDiv.scrollLeft - this.space } else {this.scrollContDiv.scrollLeft -= this.space};this.accountPageIndex()};this.leftEnd = function () {if (_state != "floating") {return};_state = "stoping";clearInterval(_scrollTimeObj);var fill = this.pageWidth - this.scrollContDiv.scrollLeft % this.pageWidth;this.move(fill)};this.rightEnd = function () {if (_state != "floating") {return};_state = "stoping";clearInterval(_scrollTimeObj);var fill = -this.scrollContDiv.scrollLeft % this.pageWidth;this.move(fill)};this.move = function (num, quick) {var thisMove = num / 5;if (!quick) {if (thisMove > this.space) {thisMove = this.space};if (thisMove < -this.space) {thisMove = -this.space}};if (Math.abs(thisMove) < 1 && thisMove != 0) {thisMove = thisMove >= 0 ? 1 : -1} else {thisMove = Math.round(thisMove)};var temp = this.scrollContDiv.scrollLeft + thisMove;if (thisMove > 0) {if (this.scrollContDiv.scrollLeft + thisMove >= this.listDiv01.scrollWidth) {this.scrollContDiv.scrollLeft = this.scrollContDiv.scrollLeft + thisMove - this.listDiv01.scrollWidth } else {this.scrollContDiv.scrollLeft += thisMove} else {if (this.scrollContDiv.scrollLeft - thisMove <= 0) {this.scrollContDiv.scrollLeft = this.listDiv01.scrollWidth + this.scrollContDiv.scrollLeft - thisMove} else {this.scrollContDiv.scrollLeft += thisMove}};num -= thisMove;if (Math.abs(num) == 0) {_state = "ready";if (this.autoPlay) {this.play()};this.accountPageIndex();return} else {this.accountPageIndex();setTimeout("ScrollPic.childs[" + this.ID + "].move(" + num + "," + quick + ")", this.speed)}};this.next = function () {if (_state != "ready") {return};_state = "stoping";this.move(this.pageWidth, true)};this.play = function () {if (!this.autoPlay) {return};clearInterval(_autoTimeObj);_autoTimeObj = setInterval("ScrollPic.childs[" + this.ID + "].next()", this.autoPlayTime * 1000)};this.stop = function () {clearInterval(_autoTimeObj)};this.pageTo = function (num) {if (_state != "ready") {return};_state = "stoping";var fill = num * this.frameWidth - this.scrollContDiv.scrollLeft;this.move(fill, true)};this.accountPageIndex = function () {this.pageIndex = Math.round(this.scrollContDiv.scrollLeft / this.frameWidth);if (this.pageIndex > Math.round(this.listDiv01.offsetWidth / this.frameWidth + 0.4) - 1) {this.pageIndex = 0};var i;for (i = 0; i < this.dotObjArr.length; i++) {if (i == this.pageIndex) {this.dotObjArr[i].className = this.dotClassName} else {this.dotObjArr[i].className = this.dotOnClassName}}}};参数说明:var scrollPic = new ScrollPic(); //定义变量,并初始化⽅法 scrollContId //滚动容器的ID arrLeftId //左按钮ID arrRightId //右按钮ID frameWidth //显⽰框宽度 pageWidth //翻页宽度 speed //移动速度(单位毫秒,越⼩越快) space //每次移动像素(单位px,越⼤越快) autoPlay //⾃动播放 autoPlayTime //⾃动播放间隔时间(秒) initialize() //初始化完整实例代码点击此处。
带左右箭头图片轮播的JS代码
带左右箭头图⽚轮播的JS代码轮播图实现效果见下图,图⽚能⾃⼰轮播,点击左右按钮进⾏翻页轮播,⿏标悬停图⽚或者标题上,停⽌轮播;效果图为:复制代码代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>带左右箭头图⽚轮播</title><style type="text/css"><!--.rollBox{width:770px;height:260px;overflow:hidden;padding:10px;margin:0 auto;border:1px solid red;}.rollBox .LeftBotton{height:40px;width:21px;background:url(images/sl.png) no-repeat 0px5px;overflow:hidden;float:left;display:inline;margin:110px 0 0 0;cursor:pointer;}.rollBox .RightBotton{height:40px;width:21px;background:url(images/sr.png) no-repeat 0px5px;overflow:hidden;float:left;display:inline;margin:110px 0 0 0;cursor:pointer;}.rollBox .Cont{width:726px;overflow:hidden;float:left;}.rollBox .ScrCont{width:10000000px;}.rollBox .Cont .pic{width:242px;float:left;text-align:center;}.rollBox .Cont .pic img{padding:9px;background:#fff;border:1px solid #ccc;display:block;margin:0auto;width:190px;height:190px;}.rollBox .Cont .pic div{line-height:20px;color:#505050;margin:15px 0px;}.rollBox .Cont .pic div span{display:block;}.rollBox .Cont a:link,.rollBox .Cont a:visited{color:#626466;text-decoration:none;}.rollBox .Cont a:hover{color:#f00;text-decoration:underline;}.rollBox #List1,.rollBox #List2{float:left;}--></style></head><body><div class="rollBox"><div class="LeftBotton" onmousedown="ISL_GoUp()" onmouseup="ISL_StopUp()" onmouseout="ISL_StopUp()"></div> <div class="Cont" id="ISL_Cont"><div class="ScrCont"><div id="List1"><!-- 图⽚列表 begin --><div class="pic"><a href="/" target="_blank"><img src="images/1.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/2.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/3.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/4.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/5.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/6.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/7.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><div class="pic"><a href="/" target="_blank"><img src="images/8.jpg" /></a><div><span><a href="https:///" target="_blank">酷站代码-特效1</a></span><span>45446547</span></div></div><!-- 图⽚列表 end --></div><div id="List2"></div></div></div><div class="RightBotton" onmousedown="ISL_GoDown()" onmouseup="ISL_StopDown()" onmouseout="ISL_StopDown()"></div></div></body><script language="javascript" type="text/javascript"><!--//--><![CDATA[//><!--//图⽚滚动列表 var Speed = 1; //速度(毫秒)var Space = 5; //每次移动(px)var PageWidth = 726; //翻页宽度var fill = 0; //整体移位var MoveLock = false;var MoveTimeObj;var Comp = 0;var AutoPlayObj = null;GetObj("List2").innerHTML = GetObj("List1").innerHTML;GetObj('ISL_Cont').scrollLeft = fill;GetObj("ISL_Cont").onmouseover = function(){clearInterval(AutoPlayObj);}GetObj("ISL_Cont").onmouseout = function(){AutoPlay();}AutoPlay();function GetObj(objName){if(document.getElementById){return eval('document.getElementById("'+objName+'")')}else{return eval('document.all.'+objName)}}function AutoPlay(){ //⾃动滚动clearInterval(AutoPlayObj);AutoPlayObj = setInterval('ISL_GoDown();ISL_StopDown();',3000); //间隔时间}function ISL_GoUp(){ //上翻开始if(MoveLock) return;clearInterval(AutoPlayObj);MoveLock = true;MoveTimeObj = setInterval('ISL_ScrUp();',Speed);}function ISL_StopUp(){ //上翻停⽌clearInterval(MoveTimeObj);if(GetObj('ISL_Cont').scrollLeft % PageWidth - fill != 0){Comp = fill - (GetObj('ISL_Cont').scrollLeft % PageWidth);CompScr();}else{MoveLock = false;}AutoPlay();}function ISL_ScrUp(){ //上翻动作if(GetObj('ISL_Cont').scrollLeft <= 0){GetObj('ISL_Cont').scrollLeft = GetObj('ISL_Cont').scrollLeft +GetObj('List1').offsetWidth}GetObj('ISL_Cont').scrollLeft -= Space ;}function ISL_GoDown(){ //下翻clearInterval(MoveTimeObj);if(MoveLock) return;clearInterval(AutoPlayObj);MoveLock = true;ISL_ScrDown();MoveTimeObj = setInterval('ISL_ScrDown()',Speed);}function ISL_StopDown(){ //下翻停⽌clearInterval(MoveTimeObj);if(GetObj('ISL_Cont').scrollLeft % PageWidth - fill != 0 ){Comp = PageWidth - GetObj('ISL_Cont').scrollLeft % PageWidth + fill;CompScr();}else{MoveLock = false;}AutoPlay();}function ISL_ScrDown(){ //下翻动作if(GetObj('ISL_Cont').scrollLeft >= GetObj('List1').scrollWidth){GetObj('ISL_Cont').scrollLeft = GetObj('ISL_Cont').scrollLeft -GetObj('List1').scrollWidth;}GetObj('ISL_Cont').scrollLeft += Space ;}function CompScr(){var num;if(Comp == 0){MoveLock = false;return;}if(Comp < 0){ //上翻if(Comp < -Space){Comp += Space;num = Space;}else{num = -Comp;Comp = 0;}GetObj('ISL_Cont').scrollLeft -= num;setTimeout('CompScr()',Speed);}else{ //下翻if(Comp > Space){Comp -= Space;num = Space;}else{num = Comp;Comp = 0;}GetObj('ISL_Cont').scrollLeft += num;setTimeout('CompScr()',Speed);}}//--><!]]></script></html>。
js实现轮播图的完整代码
js实现轮播图的完整代码今天写⼀个完整的轮播图,⾸先它需要实现三个功能:1.⿏标放在⼩圆点上实现轮播2.点击焦点按钮实现轮播3.⽆缝⾃动轮播轮播图的原理:⼀系列的⼤⼩相等的图⽚平铺,利⽤CSS布局只显⽰⼀张图⽚,其余隐藏。
通过计算偏移量(封装⼀个动画函数)⾃动播放,或通过⼿动点击事件切换图⽚。
html布局:<div id="box" class="all"><div class="inner"> <!-- 相框--><ul><li><a href="#"><img src="images/18.jpg" width="550" height="320" alt=""></a></li><li><a href="#"><img src="images/19.jpg" width="550" height="320" alt=""></a></li><li><a href="#"><img src="images/14.jpg" width="550" height="320" alt=""></a></li><li><a href="#"><img src="images/17.jpg" width="550" height="320" alt=""></a></li></ul><ol> <!--⾥⾯存放⼩圆点--></ol></div><div class="focusD" id="arr"><span id="left"><</span><span id="right">></span></div></div>css样式:* {margin: 0;padding: 0;}/*<--清除底部三像素距离-->*/img {vertical-align: top;}.all {width: 550px;height: 320px;margin: 100px auto;padding: 5px;border: 1px solid #ccc;position: relative;}.inner {position: relative;width: 550px;height: 320px;background-color: pink;overflow: hidden;}.inner ul {width: 1000%;list-style: none;position: absolute;top: 0;left: 0;}.inner ul li {float: left;}.focusD {position: absolute;left: 0;top: 50%;width: 550px;padding: 0 10px;height: 30px;box-sizing: border-box;display: none;}.inner ol {position: absolute;right: 30px;bottom: 10px;}.inner ol li {width: 15px;display: inline-block;height: 15px;margin: 0 3px;cursor: pointer;line-height: 15px;text-align: center;background-color: #fff;}/*当前的⾼亮的⼩圆点*/.inner ol .current {background-color: orange;color: #fff;}.focusD span {display: inline-block;width: 25px;font-size: 24px;height: 30px;color: #ccc;line-height: 30px;text-align: center;background: rgba(255, 255, 255, 0.3); cursor: pointer;}#left {float: left;}#right {float: right;}显⽰效果:js部分:接下来我们要写js 代码,⾸先我们先获取我们需要的所有元素。
js鼠标点击图片切换效果代码分享
js⿏标点击图⽚切换效果代码分享本⽂实例讲述了js⿏标点击图⽚切换效果。
分享给⼤家供⼤家参考。
具体如下:实现原理很简单,其实是多张图⽚叠加起来,点击图⽚后依次赋予图⽚⼀个class,使其看起来在表⾯⽽已,点击图⽚,可以实现图⽚的不断切换效果。
运⾏效果图:--------------------------------------⼩提⽰:浏览器中如果不能正常运⾏,可以尝试切换浏览模式。
为⼤家分享的js⿏标点击图⽚切换效果代码如下<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>js⿏标点击图⽚切换效果</title><style type="text/css">*{margin:0;padding:0;border:none;outline:none;list-style:none;}#wrapper {width:280px;margin:20px auto;}#imageContainer {width:280px;height:280px;position:relative;overflow:hidden;cursor:pointer;}#imageContainer img {position:absolute;top:0;left:0;z-index:1;}#imageContainer img.active {z-index:3;}</style><!--[if lt IE 9]><script src="/svn/trunk/html5.js"></script><![endif]--></head><body><div id="wrapper"><div id="imageContainer"><img src="images/01.jpg" class="active" width="280" height="280" /><img src="images/02.jpg" width="280" height="280" /><img src="images/03.jpg" width="280" height="280" /></div></div><script src="js/jquery.min.js"></script><script>var imageObject = {clickSwap : function(obj) {obj.click(function() {var activeImage = $(this).children('img.active');activeImage.removeClass('active');if (activeImage.next().length > 0) {activeImage.next().addClass('active');} else {$(this).children('img:first-child').addClass('active');}return false;});}};$(function() {imageObject.clickSwap($('#imageContainer'));});</script></body></html>以上就是为⼤家分享的js⿏标点击图⽚切换效果代码,希望⼤家可以喜欢。
JS实现自动轮播图效果(js案例)
JS实现⾃动轮播图效果(js案例)现在很多⽹站都有轮播图,这篇⽂章主要为⼤家详细介绍了js实现轮播图的完整代码及原理,需要的⼩伙伴可以参考⼀下。
1、轮播图主要功能:1、图⽚⾃动轮播(主图切换同时下⾯导航图⽚也会跟着变化)2、⿏标悬停的时候,图⽚轮播停⽌,⿏标离开后继续3、单击左右按钮切换图⽚4、⿏标移⼊后左右按钮出现,移出消失具体效果如下:⿏标移⼊:轮播图⽚数量、css样式等,⼩伙伴也可根据⾃⼰的需求做相应调整。
2、具体实现部分特别重要的是,在我们写任何动态效果之前,我们应该先把静态页⾯写出来,在考虑动态效果的实现。
HTML代码:<ul class="big_pic"><div class="prev"><a class="mark_left" href="#"></a></div><div class="next"><a class="mark_right" href="#"></a></div><div class="text">图⽚1详情</div><div class="length">1/6</div><li style="z-index: 1"><img src="images/flash_1.jpg" /></li> <li><img src="images/flash_2.jpg" /></li><li><img src="images/flash_3.jpg" /></li><li><img src="images/flash_4.jpg" /></li><li><img src="images/flash_5.jpg" /></li><li><img src="images/flash_6.jpg" /></li></ul><ul class="small_pic" ><li><img src="images/flash_1.jpg" /></li><li><img src="images/flash_2.jpg" /></li><li><img src="images/flash_3.jpg" /></li><li><img src="images/flash_4.jpg" /></li><li><img src="images/flash_5.jpg" /></li><li><img src="images/flash_6.jpg" /></li></ul></div>Css样式:@charset "utf-8";* {margin: 0;padding: 0;list-style: none;}#div1 {width: 500px;height: 420px;margin: 100px auto;position: relative;cursor: pointer;overflow: hidden;}#div1 ul.big_pic {position: relative;height: 320px;}#div1 ul.big_pic div.prev {opacity: 0;-webkit-transition: all .3s linear;transition: all .3s linear;position: absolute;left: 0;top: 0;bottom: 0;right: 50%;z-index: 100;}#div1 ul.big_pic div.prev a.mark_left {position: absolute;width: 60px;height: 60px;top: 50%;left: 10px;background: url("../images/btn.gif");}#div1 ul.big_pic div.next {opacity: 0;-webkit-transition: all .5s linear;transition: all .5s linear;position: absolute;left: 50%;top: 0;bottom: 0;right: 0;z-index: 100;}#div1 ul.big_pic div.next a.mark_right {position: absolute;height: 60px;top: 50%;right: 10px;background: url("../images/btn.gif") left -60px;}#div1 ul.big_pic div.text {position: absolute;bottom: 0;left: 0;line-height: 26px;color: white;background-color: rgba(0, 0, 0, 0.51);width: 70%;height: 26px;z-index: 200;font-size: 14px;padding-left: 20px;-webkit-box-sizing: border-box;box-sizing: border-box;}#div1 ul.big_pic div.length {position: absolute;bottom: 0;right: 0;line-height: 26px;color: white;background-color: rgba(0, 0, 0, 0.51);width: 30%;height: 26px;z-index: 200;font-size: 14px;text-align: center;}#div1 ul.big_pic li {position: absolute;top: 0;left: 0;width: 500px;height: 320px;overflow: hidden;}#div1 ul.big_pic li img {width: 100%;height: 320px;}#div1 ul.small_pic {display: -webkit-box;display: -ms-flexbox;display: flex;background-color: #b0b0b0;height: 100px;padding: 6px 5px 6px 8px;-webkit-box-sizing: border-box;box-sizing: border-box;position: absolute;}#div1 ul.small_pic li {width: calc(500px / 3);}#div1 ul.small_pic li img {width: calc(488px / 3);height: 100%;}#div1 ul.small_pic li:not(:last-child) img {padding-right: 4px;-webkit-box-sizing: border-box;box-sizing: border-box;}接下来是最重要的js代码:var oDiv=document.getElementById('div1');var oPrv=oDiv.querySelector('div.prev');var oNext=oDiv.querySelector('div.next');var oBtnPrev=oPrv.querySelector('a.mark_left'); var oBtnNext=oNext.querySelector('a.mark_right'); var oText=oDiv.querySelector('div.text');var oLength=oDiv.querySelector('div.length');var aUl=oDiv.querySelectorAll('ul');var aBigLi=aUl[0].querySelectorAll('li');var aSmallLi=aUl[1].querySelectorAll('li'); //获取标签var zIndex=1,now=0;//zIndex:主图Z轴层级,now:下⾯导航图⽚的下标(从0开始)oBtnNext.onclick=function () {now++;if(now===aSmallLi.length) now=0;//当now等于⼩导航图⽚的长度时,把主图⽚换成第⼀张opublic();};oBtnPrev.onclick=function () {now--;if(now===-1) now=aSmallLi.length-1;//当now等于-1,把主图⽚换成最后opublic();};for (var i=0;i<aSmallLi.length;i++){aSmallLi[i].style.opacity=.6;//统⼀设置下⾯导航图⽚透明度为0.6aSmallLi[0].style.opacity=1;//初始化第⼀个导航图⽚透明度为1aSmallLi[i].index=i;//为每⼀个导航图加⼀个index的⾃定义属性aSmallLi[i].onclick=function () {//添加单击事件now=this.index;//当单击图⽚后,主图切换为对应图⽚opublic();};}function opublic(){//公共部分aBigLi[now].style.zIndex=zIndex++;//图⽚切换改变Z轴层级aBigLi[now].style.height=0;startMove(aBigLi[now],'height',320);//图⽚⾼度从0->360,实现图⽚动态叠加效果oText.innerHTML='图⽚'+(now+1)+'详情';oLength.innerHTML=(now+1)+'/'+aBigLi.length;for(var i=0;i<aSmallLi.length;i++){startMove(aSmallLi[i], 'opacity', 60);}startMove(aSmallLi[now], 'opacity', 100);if(now===0){startMove(aUl[1], 'left', 0);}else if(now===aSmallLi.length-1){//当当前导航图⽚为最后⼀张时,固定最后⼀张图⽚位置startMove(aUl[1], 'left', -(now-2)*aSmallLi[0].offsetWidth);}else{//下⾯导航图⽚的轮播效果startMove(aUl[1], 'left', -(now-1)*aSmallLi[0].offsetWidth);}}oPrv.onmouseover=oNext.onmouseover=function () {//⿏标移⼊左右按钮出现this.style.opacity=1;};oPrv.onmouseout=oNext.onmouseout=function () {//⿏标移出左右按钮消失this.style.opacity=0;};var timer=setInterval(oBtnNext.onclick, 3000);//设置⼀个定时器,每3秒钟模拟⼀次右边按钮的单击事件 oDiv.onmouseenter=function () {//⿏标移⼊后,定时器取消clearInterval(timer);};oDiv.onmouseleave=function () {//⿏标离开后开启定时器timer=setInterval(oBtnNext.onclick, 3000);}function startMove(obj, attr, iTarget)//运动框架,// obj:运动的对象,attr:运动的属性,iTarget:⽬标值{clearInterval(obj.timer);obj.timer=setInterval(function (){var cur=0;if(attr==='opacity'){cur=Math.round(parseFloat(getStyle(obj, attr))*100);}else{cur=parseInt(getStyle(obj, attr));}var speed=(iTarget-cur)/10;speed=speed>0?Math.ceil(speed):Math.floor(speed);if(cur==iTarget){clearInterval(obj.timer);}else{if(attr=='opacity'){obj.style.filter='alpha(opacity:'+(cur+speed)+')'; obj.style.opacity=(cur+speed)/100;}else{obj.style[attr]=cur+speed+'px';}}}, 30);}。
基于jquery实现左右按钮点击的图片切换效果
基于jquery实现左右按钮点击的图⽚切换效果jQuery可以制作出与Flash媲美的动画效果,这点绝对⽏庸置疑,本⽂将通过实例演⽰⼀个左右按钮点击的图⽚切换效果。
⼀、最终效果⼆、功能分析1、需求分析点击左边pre按钮,显⽰前⾯三个图⽚,点击右边的next按钮,显⽰后⾯的⼀组(三个)图⽚。
初始化只显⽰next按钮,到最后⼀组只显⽰pre按钮,中间过程两按钮都显⽰。
2、html结构分析<div class="activity" id="activity-slide"><a href="javascript:void(0)" class="pg_left ps_pre"></a><a href="javascript:void(0)" class="pg_right ps_next" ></a><ul class="clearfix"><li><a href="javascript:;"><img src="images/activity01-1410.jpg"></a></li><li><a href="javascript:;"><img src="images/activity02-1410.jpg"></a></li><li><a href="javascript:;"><img src="images/activity03-1410.jpg"></a></li><li><a href="javascript:;"><img src="images/activity03-1410.jpg"></a></li><li><a href="javascript:;"><img src="images/activity02-1410.jpg"></a></li></ul></div>#activity-slide是整个幻灯的⼊⼝,后⾯会将其作为参数来调⽤幻灯功能。
带向左向右方向按钮图片无限循环左右滚动JS代码
带向左向右方向按钮图片无限循环左右滚动JS代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><html xmlns=""><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>无标题文档</title><script type="text/javascript" src="/ajax/libs/jquery/1.4.1/jquery.js"></script><style type="text/css">body,ul,li { padding:0; margin:0}ul,li { list-style:none}.img-scroll { position:relative; margin:20px auto; width:440px;} .img-scroll .prev,.img-scroll .next { position:absolute; display:block; width:50px; height:100px; background-color:#000;top:0; color:#FFF; text-align:center; line-height:100px}.img-scroll .prev { left:0}.img-scroll .next { right:0}.img-list { position:relative; width:320px; height:100px; margin-left:60px; overflow:hidden}.img-list ul { width:9999px;}.img-list li { float:left; display:inline; width:100px; margin-right:10px; height:100px; background-color:#BDBDDF; text-align:center; line-height:100px;}</style></head><body><div class="img-scroll"><span class="prev">prev</span><span class="next">next</span><div class="img-list"><ul><li>阿</li><li>里</li><li>西</li><li>西</li></ul></div></div><script type="text/javascript">function DY_scroll(wraper,prev,next,img,speed,or) {var wraper = $(wraper);var prev = $(prev);var next = $(next);var img = $(img).find('ul');var w = img.find('li').outerWidth(true);var s = speed;next.click(function(){img.animate({'margin-left':-w},function(){img.find('li').eq(0).appendTo(img);img.css({'margin-left':0});});});prev.click(function(){img.find('li:last').prependT o(img);img.css({'margin-left':-w});img.animate({'margin-left':0});});if (or == true){ad = setInterval(function() { next.click();},s*1000);wraper.hover(function(){clearInterval(ad);},function(){ad = setInterval(function() { next.click();},s*1000);});}}DY_scroll('.img-scroll','.prev','.next','.img-list',3,true);// true为自动播放,不加此参数或false就默认不自动</script></body></html>。
使用jQuery做左右点击切换图片的手动图片轮播效果
使⽤jQuery做左右点击切换图⽚的⼿动图⽚轮播效果代码和个⼈解析:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title><style>img {width: 300px;height: 300px;} /* 这⾥设置样式只是为了使图⽚在点击时不会出现图⽚⼤⼩不⼀的情况,img可以统⼀设置页⾯中所有的图⽚⼤⼩ */</style></head><body><img src="./img/1.jpg" /> <!-- 这⾥有⼀个要注意的点,设置相对的路径,图⽚的名称要变成连贯的数字序号排列 --><button>上⼀张</button><button>下⼀张</button><script src="./js/jquery-3.4.1.js"></script> // 引⼊jQuery⽂件<script>$(function() {var t = 1; //定义⼀个变量⽤来进⾏判断//上⼀张的按钮实现$("button").eq(0).click(function() { //这⾥的eq()是相当于是索引,从0开始计数,就是获取第⼀个button按钮t += 1; //我这⾥是简写了,展开就是 t=t+1 ,当点击第⼆次的时候,就+1,把图⽚变成第⼆张,再点击,就再+1 if (t > 4) { //这⾥的if判断,也可以说是充当⼀个for循环,我只有4张图⽚,当我点击到最后⼀张时,就是说图⽚已经到我没有的第五张图时, //就把t的值改为 1 就到了我的第⼀张图⽚,类似于⼀个for循环t = 1}$("img").attr("src", "img/" + t + ".jpg") //这⾥是⼀个类似字符串的拼接,t 是⼀个动态的数据})//下⼀张的按钮实现$("button").eq(1).click(function() {t -= 1; //展开 t=t-1if (t < 1) {t = 4}$("img").attr("src", "img/" + t + ".jpg")})//下⼀张按钮的实现与上⼀张的思路和逻辑是⼀样,判断就是把上⼀张按钮的条件反过来//值得注意的是eq()的索引值的起始值,还有把判断条件理清楚,然后就是图⽚的命名})</script></body></html><!-- ⼀起加油努⼒学习前端知识吧 -->。
js实现轮播图效果纯js实现图片自动切换
js实现轮播图效果纯js实现图⽚⾃动切换本⽂实例为⼤家分享了纯js实现图⽚⾃动切换的具体代码,供⼤家参考,具体内容如下1.⿏标经过的时候左右两个⼩按钮会⾃动弹出,⾃动播放停⽌,点击左右⼩按钮可以切换图⽚;2. ⿏标离开,恢复⾃动播放;3. 点击下⽅中间⼏个⼩圆圈,也会⾃动切换图⽚;源代码:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title><style>* {margin: 0;padding: 0;}/* 轮播图盒⼦样式 */.lunbotu {position: relative;width: 520px;height: 280px;margin: 50px auto;background-color: blue;overflow: hidden;}/* 左右按钮样式 */.left,.right {display: none;position: absolute;top: 50%;margin-top: -15px;width: 30px;height: 30px;background-color: cornsilk;border-radius: 15px;text-align: center;line-height: 30px;cursor: pointer;z-index: 1;}.left {left: 0;}.right {right: 0;}li {list-style: none;}/* 设置图⽚的ul的样式 */.firstul {position: absolute;top: 0;left: 0;width: 500%;}.firstul li {float: left;/* display: none; */}/* 设置⼩圆圈的样式 */ol {/* width: 90px; */padding: 0 5px 0 5px;position: absolute;bottom: 10px;left: 50%;margin-left: -45px;background-color: darkgrey;text-align: center;border-radius: 9px;}ol li {display: inline-block;width: 15px;height: 15px;border-radius: 50%;margin-right: 5px;background-color: white;cursor: pointer;}.current {background-color: red;}</style><script src="animation.js"></script></head><body><!-- 图⽚⼤⼩全部是520*280 --><div class="lunbotu"><!-- 左右按钮 --><div class="left">></div><div class="right"><</div><!-- 图⽚部分 --><ul class="firstul"><li><a href=""><img src=" images/1.jpg" alt=""> </a></li><li><a href=""><img src=" images/2.jpg" alt=""> </a></li><li><a href=""><img src=" images/3.gif" alt=""> </a></li><li><a href=""><img src=" images/4.webp" alt=""> </a></li> </ul><!-- ⼩圆圈 --><ol class="firstol"></ol></div><!-- JS部分 --><script>// 1.获取事件源var lunbotu = document.querySelector('.lunbotu');var leftBox = document.querySelector('.left');var rightBox = document.querySelector('.right');var ul = lunbotu.querySelector('ul');var ol = lunbotu.querySelector('ol');var right = document.querySelector('.right');var left = document.querySelector('.left');var lunbotuWidth = lunbotu.offsetWidth;// console.log(ul)// console.log(ol)// 第⼀步:// ⿏标经过轮播图的时候,左右⼩按钮弹出lunbotu.addEventListener('mouseenter', function () {leftBox.style.display = 'block';rightBox.style.display = 'block';// ⿏标经过轮播图的时候,停⽌定时器clearInterval(timer);})// ⿏标离开轮播图的时候,左右⼩按钮隐藏lunbotu.addEventListener('mouseleave', function () {leftBox.style.display = 'none';rightBox.style.display = 'none';timer = setInterval(function () {right.click();}, 2000)})// 第⼆步:// 1.动态⽣成⼩圆圈// 2.⼩圆圈的个数要跟图⽚⼀样// 3.先得到ul⾥⾯图⽚的张数(图⽚放⼊li⾥⾯,所以就是li的个数) // 4.利⽤循环动态⽣成⼩圆圈(这个⼩圆圈要放⼊ol⾥⾯)// 5.创建节点createElement('li')]// 6.插⼊节点ol.appendChild(li)// 7.第⼀个⼩圆圈需要添加current类for (var i = 0; i < ul.children.length; i++) {// 创建⼀个livar li = document.createElement('li')// 记录当前⼩圆圈的索引号通过⾃定义属性来做li.setAttribute('index', i);// 把li添加到olol.appendChild(li);}// 排他思想:让⼩Li变⽩⾊for (var i = 0; i < ol.children.length; i++) {ol.children[i].addEventListener('click', function () {for (var i = 0; i < ol.children.length; i++) {ol.children[i].className = '';} this.className = 'current';// 点击⼩圆圈的时候切换到对应的图⽚// 得到索引号 indexvar index = this.getAttribute('index');// 解决⼩bugnum = index;num_ol = index;// console.log(lunbotuWidth);// console.log(index)animation(ul, - index * lunbotuWidth)})}// 给第⼀个li变颜⾊ol.children[0].className = 'current';// 克隆第⼀个livar first = ul.children[0].cloneNode(true);ul.appendChild(first);// 第三步:// 点击右边按钮事件var num = 0;// 点击右侧按钮的时候⼩圆圈跟着滚动var num_ol = 0;// 节流阀,防⽌点击过快,最后才加这句优化var flag = true;// 右侧按钮:right.addEventListener('click', function () {if (flag) {flag = false; // 关闭节流阀if (num == ul.children.length - 1) {ul.style.left = 0;num = 0;}num++;animation(ul, -num * lunbotuWidth, function () {flag = true;});num_ol++;if (num_ol == ol.children.length) {num_ol = 0}for (var i = 0; i < ol.children.length; i++) {ol.children[i].className = '';}ol.children[num_ol].className = 'current';}});// 左侧按钮:left.addEventListener('click', function () {if (flag) {flag = false;if (num == 0) {ul.style.left = -(ul.children.length - 1) * lunbotuWidth + 'px';num = ul.children.length - 1;}num--;animation(ul, -num * lunbotuWidth, function () {flag = true;});num_ol--;// num_ol=0的时候需要,点击左侧按钮,需要转跳到ol.children.length-1的位置 if (num_ol < 0) {num_ol = ol.children.length - 1}for (var i = 0; i < ol.children.length; i++) {ol.children[i].className = '';}ol.children[num_ol].className = 'current';}});// ⾃动播放图⽚var timer = setInterval(function () {right.click();}, 2000)</script></body></html>5.Js⽂件的代码// 封装了⼀个动画js⽂件function animation(obj,target,fn1){// console.log(fn1);// fn是⼀个回调函数,在定时器结束的时候添加// 每次开定时器之前先清除掉定时器clearInterval( obj.timer);obj.timer = setInterval(function(){// 步长计算公式越来越⼩// 步长取整var step = (target - obj.offsetLeft) /10;step = step > 0 ? Math.ceil(step) :Math.floor(step);if(obj.offsetLeft == target){clearInterval( obj.timer);// 如果fn1存在,调⽤fnif(fn1){fn1();}}else{// 每50毫秒就将新的值给obj.leftobj.style.left = obj.offsetLeft +step +'px';}},30)}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
javascript实现点击按钮切换轮播图功能
javascript实现点击按钮切换轮播图功能本⽂实例为⼤家分享了js实现点击按钮切换轮播图的具体代码,供⼤家参考,具体内容如下菜单区域实现划过主菜单显⽰⼦菜单轮播区域实现1、点击图⽚中左右箭头,分别跳转上⼀张与下⼀张(1)点击上⼀张图其实就是让⼀个变量进⾏递减,点击下⼀张图就是让变量递增;(控制索引最⼤最⼩值)2、点击右下⾓⼩圆也可以进⾏图⽚的跳转(1)通过索引让变量进⾏随意的修改3、每间隔3s进⾏轮播图的⾃动切换,⿏标放在图⽚上清除⾃动切换(1)定时器4、⿏标放在主菜单上显⽰⼦菜单,离开⼦菜单消失。
放在⼦菜单上显⽰⼦菜单,离开消失。
源代码:html<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>轮播图</title><link rel="stylesheet" type="text/css" href="css/style.css" /><script type="text/javascript" src="js/script.js"></script></head><body><div class="main" id="main"><!-- 主菜单背景 --><div class="menu-box"></div><!-- 主菜单 --><div class="menu-content" id="menu-content"><div class="menu-item"><a href=""><span>⼿机、配件</span><i class="icon"></i></a></div><div class="menu-item"><a href=""><span>电脑</span><i class="icon"></i></a></div><div class="menu-item"><a href=""><span>家⽤电器</span><i class="icon"></i></a></div><div class="menu-item"><a href=""><span>家具</span><i class="icon"></i></a></div></div><!-- ⼦菜单 --><div class="sub-menu hide" id="sub-menu"><!-- ⼦菜单背景 --><div class="inner-box"><!-- ⼦菜单内容 --><div class="sub-inner-box"><div class="title">⼿机、配件</div><div class="sub-row"><span class="bold mr10">⼿机通讯:</span><a href="">⼿机</a><span class="ml10 mr10">/</span><a href="">⼿机维修</a><span class="ml10 mr10">/</span><a href="">以旧换新</a></div><div class="sub-row"><span class="bold mr10">⼿机配件:</span><a href="">⼿机壳</a><span class="ml10 mr10">/</span><a href="">⼿机储蓄卡</a><span class="ml10 mr10">/</span><a href="">数据线</a><span class="ml10 mr10">/</span><a href="">充电器</a><span class="ml10 mr10">/</span><a href="">电池</a></div><div class="sub-row"><span class="bold mr10">运营商:</span> <a href="">中国联通</a><span class="ml10 mr10">/</span><a href="">中国移动</a><span class="ml10 mr10">/</span><a href="">中国电信</a></div><div class="sub-row"><span class="bold mr10">智能设备:</span> <a href="">智能⼿环</a><span class="ml10 mr10">/</span><a href="">智能家居</a><span class="ml10 mr10">/</span><a href="">智能⼿表</a><span class="ml10 mr10">/</span><a href="">其他配件</a></div><div class="sub-row"><span class="bold mr10">娱乐:</span><a href="">⽿机</a><span class="ml10 mr10">/</span><a href="">⾳响</a><span class="ml10 mr10">/</span><a href="">收⾳机</a><span class="ml10 mr10">/</span><a href="">麦克风</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">电脑</div><div class="sub-row"><span class="bold mr10">电脑:</span><a href="">笔记本</a><span class="ml10 mr10">/</span><a href="">平板</a><span class="ml10 mr10">/</span><a href="">⼀体机</a></div><div class="sub-row"><span class="bold mr10">电脑配件:</span> <a href="">显⽰器</a><span class="ml10 mr10">/</span><a href="">CPU</a><span class="ml10 mr10">/</span><a href="">主板</a><span class="ml10 mr10">/</span><a href="">硬盘</a><span class="ml10 mr10">/</span><a href="">电源</a><span class="ml10 mr10">/</span><a href="">显卡</a><span class="ml10 mr10">/</span><a href="">其他配件</a></div><div class="sub-row"><span class="bold mr10">游戏设备:</span> <a href="">游戏机</a><span class="ml10 mr10">/</span><a href="">⽿机</a><span class="ml10 mr10">/</span><a href="">游戏软件</a></div><div class="sub-row"><span class="bold mr10">⽹络产品:</span> <a href="">路由器</a><span class="ml10 mr10">/</span><a href="">⽹络机顶盒</a><span class="ml10 mr10">/</span><a href="">交换机</a><span class="ml10 mr10">/</span><a href="">存储卡</a><span class="ml10 mr10">/</span><a href="">⽹卡</a></div><div class="sub-row"><span class="bold mr10">外部产品:</span> <a href="">⿏标</a><span class="ml10 mr10">/</span><a href="">键盘</a><span class="ml10 mr10">/</span><a href="">U盘</a><span class="ml10 mr10">/</span><a href="">移动硬盘</a><span class="ml10 mr10">/</span><a href="">⿏标垫</a><span class="ml10 mr10">/</span><a href="">电脑清洁</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">家⽤电器</div><div class="sub-row"><span class="bold mr10">电视:</span><a href="">国产品牌</a><span class="ml10 mr10">/</span><a href="">韩国品牌</a><span class="ml10 mr10">/</span><a href="">欧美品牌</a></div><div class="sub-row"><span class="bold mr10">空调:</span><a href="">显⽰器</a><span class="ml10 mr10">/</span><a href="">柜式</a><span class="ml10 mr10">/</span><a href="">中央</a><span class="ml10 mr10">/</span><a href="">壁挂式</a></div><div class="sub-row"><span class="bold mr10">冰箱:</span><a href="">多门</a><span class="ml10 mr10">/</span><a href="">对开门</a><span class="ml10 mr10">/</span><a href="">三门</a><span class="ml10 mr10">/</span><a href="">双门</a></div><div class="sub-row"><span class="bold mr10">洗⾐机:</span> <a href="">滚筒式洗⾐机</a><span class="ml10 mr10">/</span><a href="">迷你洗⾐机</a><span class="ml10 mr10">/</span><a href="">洗烘⼀体机</a></div><div class="sub-row"><span class="bold mr10">厨房电器:</span> <a href="">抽烟机</a><span class="ml10 mr10">/</span><a href="">洗碗机</a><span class="ml10 mr10">/</span><a href="">燃⽓灶</a></div></div></div><div class="inner-box"><div class="sub-inner-box"><div class="title">家具</div><div class="sub-row"><span class="bold mr10">家纺:</span><a href="">被⼦</a><span class="ml10 mr10">/</span><a href="">枕头</a><span class="ml10 mr10">/</span><a href="">四件套</a><span class="ml10 mr10">/</span><a href="">床垫</a></div><div class="sub-row"><span class="bold mr10">灯具:</span><a href="">台灯</a><span class="ml10 mr10">/</span><a href="">顶灯</a><span class="ml10 mr10">/</span><a href="">节能灯</a><span class="ml10 mr10">/</span><a href="">应急灯</a></div><div class="sub-row"><span class="bold mr10">厨具:</span><a href="">烹饪锅具</a><span class="ml10 mr10">/</span><a href="">餐具</a><span class="ml10 mr10">/</span><a href="">菜板⼑具</a></div><div class="sub-row"><span class="bold mr10">家装:</span><a href="">地毯</a><span class="ml10 mr10">/</span><a href="">沙发垫套</a><span class="ml10 mr10">/</span><a href="">装饰字画</a><span class="ml10 mr10">/</span><a href="">照⽚墙</a><span class="ml10 mr10">/</span><a href="">窗帘</a></div><div class="sub-row"><span class="bold mr10">⽣活⽇⽤:</span> <a href="">收纳⽤品</a><span class="ml10 mr10">/</span><a href="">浴室⽤品</a><span class="ml10 mr10">/</span><a href="">⾬伞⾬⾐</a></div></div></div></div><!-- 焦点图 --><div class="banner" id="banner"><!-- ⿏标悬停停⽌⾃动轮播的区域 --><section class="cease" id="cease"></section><a href=""><div class="banner-slide slide1"></div></a><a href=""><div class="banner-slide slide2"></div></a><a href=""><div class="banner-slide slide3"></div></a></div><!-- 按钮 --><a href="javascript:void(0)" class="button prev" id="prev"></a><a href="javascript:void(0)" class="button next" id="next"></a><div class="dots" id="dots"><span class="active"></span><span></span><span></span></div></div></body></html>css*{margin: 0;padding: 0;}/*通配符选择器,外边距,内边距为0*/body{font-family: "微软雅⿊";color: #14191e;}/*字体,字体颜⾊*/@font-face{font-family: 'iconfont';/*⾃定义名称,要有意义,不准数字*/src: url('../font/iconfont.eot');/*兼容ie9以上版本*/src: url('../font/iconfont.eot?#iefix') format('embedded-opentype'),/*兼容ie6-8版本*/ url('../font/iconfont.ttf') format('truetype'),/*主要针对⼿机端浏览器 safari android ios*/ url('../font/iconfont.woff') format('woff'),/*兼容所有浏览器 */url('../font/iconfont.svg#iconfont') format('svg');/*针对ios端开发 legacy ios*//* font-weight: bold;<字体加粗>font-style: normal;默认的正常显⽰*/}/*获取下载的字体*/a{text-decoration: none;}/*没有下划线*/a:link,a:visited{color: #5e5e5e;}/*联合选择器,未访问连接时与已经访问链接时的字体颜⾊*/.main{width: 1200px;height: 460px;margin: 30px auto;overflow: hidden;position: relative;}/*最⼤框架:宽;⾼;外边距,上下30px,左右居中;溢出的部分隐藏;相对定位*/ .banner{width: 1200px;height: 460px;overflow: hidden;cursor: pointer;}/*焦点图:宽;⾼;溢出的部分隐藏;⼩⼿标志*/.banner-slide{width: 1200px;height: 460px;float: right;background-repeat: no-repeat;display: none;}/*三个图⽚:宽;⾼;右浮动,背景不重复;全部隐藏*/.slide1{background-image: url(../img/bg1.jpg);display: block;}/*第⼀个图⽚;块级元素显⽰*/.slide2{background-image: url(../img/bg2.jpg);}/*第⼆个图⽚*/.slide3{background-image: url(../img/bg3.jpg);}/*第三个图⽚*/.button{position: absolute;width: 40px;height: 80px;right: 0;background: url(../img/arrow.png) center center no-repeat;top: 50%;margin-top: -40px;}/*左右两个按钮;宽;⾼;相对main绝对定位,距右0,距上50%;背景图⽚,⽔平垂直居中,不重复;外上边距-40px;*/.button:hover{background-color: #333;opacity: 0.8;filter: alpha(opacity=80);}/*⿏标悬浮在上⾯时状态:背景颜⾊,透明度。
html无缝轮播图完整代码
html⽆缝轮播图完整代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>轮播图⽆缝滚动</title><style type="text/css">*{ padding:0; margin:0; list-style:none; border:0;}.all{width:500px;height:200px;padding:7px;border:1px solid #ccc;margin:100px auto;position:relative;}.screen{width:500px;height:200px;overflow:hidden;position:relative;}.screen li{ width:500px; height:200px; overflow:hidden; float:left;}.screen ul{ position:absolute; left:0; top:0px; width:3000px;}.all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}.all ol li{ float:left; width:20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}.all ol li.current{ background:yellow;}/*#arr {display: none;}*/#arr span{ width:40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'⿊体'; font-size:30px; color:#fff; opacity:0.3; #arr #right{right:5px; left:auto;}</style></head><body><div class="all" id='all'><div class="screen" id="screen"><ul id="ul"><li><img src="images/1.jpg" width="500" height="200"/></li><li><img src="images/2.jpg" width="500" height="200"/></li><li><img src="images/3.jpg" width="500" height="200"/></li><li><img src="images/4.jpg" width="500" height="200"/></li><li><img src="images/5.jpg" width="500" height="200"/></li></ul><ol></ol><div id="arr"><span id="left"><</span><span id="right">></span></div></div></div></body></html><script>//需求:⽆缝轮播图//步骤://1.⽼三步。
jquery实现左右轮播图效果
jquery实现左右轮播图效果
我们经常看到购物⽹站上⾸页有轮播图,⽐如某东某京
如图我们打开⽹页就看到京东的轮播图
接下来,这个⼩案例就是要介绍如何实现这种效果,即这张轮播图上所有的事件.
⾸先我们看到轮播图上的有1.左右的⼩按钮(图⽚会随着左右的点击移动) 2**重点内容.还有位于中间的⼩圆点(点击⼩圆点会跳转到那个图⽚) 3重点内容**轮播图会⾃⼰向左滑动(⾥⾯有⼀个定时器⾃动播放)
分析完之后就是我们写代码(这个是仿京东的轮播图)
HTML的内容
代码⽚
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
原生js解决图片点击左右切换(简单轮播图)
原⽣js解决图⽚点击左右切换(简单轮播图)想写⼀个综合性的⼩案例,主要会运⽤到数组和判断以及我前⾯⼏篇博客所复习到的js的知识。
今天案例想要实现的效果图如下图所⽰:效果是:点击“循环切换”按钮,那么“⼀号”位置的⽂案就要写⼊“图⽚可以循环”,⽽下⾯的左右箭头在点击过程中可以循环点击,并且“⼆号”和“三号”要响应切换到相对应的数字和⽂字内容;否则,点击“顺序切换”按钮,那么“⼀号”位置的⽂案就要写⼊“图⽚是顺序播放”除了“⼆号”和“三号”要响应切换到相对应的数字和⽂字内容之外,⽆论是第⼀张还是最后⼀张都不能继续往下点击了。
好的具体来看代码。
先来简单的看⼀下布局和样式:1<style>2 body{text-align: center}3 #box{width:400px;height:400px;border:5px solid #e5e5e5;margin:20px auto;position:relative;}4 #img{width:100%;height:100%;}5 #text1{width:100%;height:30px;position:absolute;left:0;top:0;background: #000;text-align:center; line-height:30px;color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8;}6 #text2{width:100%;height:30px;position:absolute;left:0;bottom:0;background: #000;text-align:center; line-height:30px; color:#fff; font-size:14px; filter:alpha(opacity:80); opacity:0.8;}7 a{width:40px; height:40px; background:#fff; filter:alpha(opacity:80); opacity:0.8; position:absolute; top:160px; font-size:18px; color:#000; text-align:center; line-height:40px; text-decoration:none; }8 a:hover { filter:alpha(opacity:30); opacity:0.3; }9 #prev {left:10px;}10 #next {right:10px;}11</style>1<body>2<input id="btn1" type="button" value="循环切换">3<input id="btn2" type="button" value="顺序切换">4<p id="p1">图⽚可以循环</p>5<div id="box">6<img id="img"/>7<p id="text1"></p>8<p id="text2">图⽚⽂字加载中……</p>9<a id="prev" href="javascript:void(0)"><</a>10<a id="next" href="javascript:void(0)">></a>11</div>12</body>接下来就是最重要的js的写法了,因为是原⽣的写法,所以只能先获取到所有能⽤到的id,然后再去写具体的效果:1 <script>2 window.onload= function(){3var oBtn1=document.getElementById("btn1");4var oBtn2=document.getElementById("btn2");5var oBox=document.getElementById("box");6var oImg=document.getElementById("img");7var oText1=document.getElementById("text1");8var p1=document.getElementById("p1");9var oText2=document.getElementById("text2");10var oPrev=document.getElementById("prev");11var oNext=document.getElementById("next");12var arrSrc=["img/pic1.jpg","img/pic2.jpg","img/pic3.jpg","img/pic4.jpg"];13var arrTxt=["⽂案1","⽂案2","⽂案3","⽂案4"];14var num=0;15var onOff=true; //true 循环 false ⾛顺序1617function fn(){ //开始先定义⼀个公共函数⽅便下⾯调⽤18 oImg.src = arrSrc[num]; //图⽚的显⽰路径19 oText1.innerHTML= num+1 +"/" + arrTxt.length; //图中⼆号位置的数字显⽰20 oText2.innerHTML=arrTxt[num]; //图中三号位置的⽂字显⽰21 }22 fn();/*切记,定义之后,这⾥⼀定要调⽤⼀下*/2324//循环按钮25 oBtn1.onclick=function(){26 onOff=true;27 p1.innerHTML="图⽚会循环"2829 };30//顺序按钮31 oBtn2.onclick=function(){32 onOff=false;33 p1.innerHTML = "图⽚按顺序⾛"3435 };36 oPrev.onclick = function(){37 num--; //点击左边的箭头那么就是num--38if(onOff){ //onOff默认是true所以就是⾛循环了39if(num==-1){ //在⼀直减的过程中,数组⾥最⼩的就是0,所以等于-1的时候要循环下去所以让它显⽰最后⼀张40 num=arrSrc.length-1;41 }4243 }else{ //否则onOff是false所以就是⾛顺序了44if(num==-1){ //在⼀直减的过程中,数组⾥最⼩的就是0,所以等于-1的时候没有数据⾛了,所以让它显⽰第⼀张不能点击了45 num=0;46 alert("已经是第⼀张了")47 }48 }49 fn();//判断好之后记得调⽤函数50 };51//相反下⾯就是点击左边的箭头所做出的判断,道理同上52 oNext.onclick = function(){53 num++;54if(onOff){55if(num==arrTxt.length){56 num=0;57 }58 }else{59if(num==arrTxt.length){60 num=arrSrc.length-1;61 alert("已经是最后⼀张了")62 }63 }64 fn();65 }66 }67 </script>好了,以上就是这个案例所有代码了,这⾥⾯牵涉到两层逻辑关系就是点击最上⾯的循环和顺序的按钮时,下⾯的图⽚所对应到相应的效果,第⼆个逻辑就是当确定好是循环或者是顺序播放的其中⼀个之后,下⾯的图⽚在点击切换的时候是判断循环的⼀定要可以让它⼀直点击下去,⽽顺序的就是点击到最后⼀张就不能⾛下去了,要显⽰已经到头了,其实这些明⽩之后就好说了。
js鼠标点击按钮切换图片-图片自动切换-点击左右按钮切换特效代码
js⿏标点击按钮切换图⽚-图⽚⾃动切换-点击左右按钮切换特效代码今天来分享⼀下⿏标点击按钮,图⽚进⾏切换+图⽚⾃动切换+点击左右按钮图⽚进⾏切换的三种效果的组合代码。
最后的效果如下:Html代码部分:<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="Generator" content="EditPlus®"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""><title>图⽚轮播效果制作_赵⼀鸣随笔博客</title><link rel="stylesheet" type="text/css" href="css/style.css"><script type="text/javascript" src="js/jquery.js"></script><script type="text/javascript" src="js/style.js"></script></head><body><div class="banner"><ul class="pic"><li><a href="#"><img src="images/1.jpg" alt="美⼥" width="350" height="495"></a></li><li><a href="#"><img src="images/2.jpg" alt="美⼥" width="350" height="495"></a></li><li><a href="#"><img src="images/3.jpg" alt="美⼥" width="350" height="495"></a></li><li><a href="#"><img src="images/4.jpg" alt="美⼥" width="350" height="495"></a></li><li><a href="#"><img src="images/5.jpg" alt="美⼥" width="350" height="495"></a></li></ul><ul class="anniu"><li class="on"></li><li></li><li></li><li></li><li></li></ul><ul class="lr"><li class="pre"><a href="#"> < </a></li><li class="next"><a href="#"> > </a></li></ul></div></body></html>Css代码部分:*{margin:0px;padding:0px}li{list-style:none}a{text-decoration:none}img{border:0px}.banner{width:350px;height:495px;margin:100px auto;position:relative;overflow:hidden}.banner .pic{width:9999px;height:495px}.banner .pic li{width:350px;height:495px;float:left}.banner .anniu{width:100px;height:16px;position:absolute;left:165px;top:470px}.banner .anniu li{width:16px;height:16px;background:white;float:left;margin:2px;display:inline;cursor:pointer;border-radius:100%}.banner .anniu li.on{background:red}.banner .lr{width:350px;height:50px;position:absolute;top:250px;display:none}.banner .lr a{color:white}.banner .lr .pre{width:20px;height:50px;float:left;background:none repeat scroll 0px 0px rgba(1, 0, 0, 0.6);text-align:center;line-height:50px;cursor:pointer} .banner .lr .next{width:20px;height:50px;float:right;background:none repeat scroll 0px 0px rgba(1, 0, 0, 0.6);text-align:center;line-height:50px;cursor:pointer} Javascript代码部分:$(function(){//⿏标滑过banner,左右按钮进⾏显⽰和隐藏$(".banner").hover(function(){$(".lr").show();},function(){$(".lr").hide();});//点击下⾯的⼩按钮,图⽚进⾏左右切换效果$(".anniu li").click(function(){$(this).addClass("on").siblings().removeClass("on");var num=$(this).index();$(".pic").animate({marginLeft:-350*num},"slow");});//图⽚⾃动轮播效果var a=0;var automatic=setInterval(function(){a++;a=a%5;$(".pic").animate({marginLeft:-350*a},"slow");$(".anniu li").eq(a).addClass("on").siblings().removeClass("on");},6000);//点击左右按钮,图⽚进⾏切换效果$(".pre").click(function(){a--;a=(a+5)%5;$(".pic").animate({marginLeft:-350*a},"slow");$(".anniu li").eq(a).addClass("on").siblings().removeClass("on");});$(".next").click(function(){a++;a=a%5;$(".pic").animate({marginLeft:-350*a},"slow");$(".anniu li").eq(a).addClass("on").siblings().removeClass("on");});});。
jQuery幻灯片特效代码分享鼠标滑过按钮时切换(2)
jQuery幻灯⽚特效代码分享⿏标滑过按钮时切换(2)这篇⽂章主要介绍了jQuery实现幻灯⽚焦点图,可实现⾮常炫⽬时尚的幻灯⽚效果,⾮常具有实⽤价值,基本能满⾜你在⽹页上使⽤幻灯⽚(焦点图)效果,具体如下幻灯⽚效果描述:⽤⿏标滑过右下⾓数字按钮幻灯⽚进⾏左右切换进⾏切换。
⾃定义切换参数效果:向下切换、切换时间为8秒、⿏标滑过按钮时切换运⾏效果截图如下:具体代码如下<head><title>Jquery幻灯⽚焦点图插件</title><script src="js/jquery-1.4a2.min.js" type="text/javascript"></script><script src="js/jquery.-1.2.1.min.js" type="text/javascript"></script><script type="text/javascript">$(function(){$("#KinSlideshow").KinSlideshow({moveStyle:"down",intervalTime:8,mouseEvent:"mouseover",titleFont:{TitleFont_size:14,TitleFont_color:"#FF0000"}});})</script><style type="text/css">.code{ height:auto; padding:20px; border:1px solid #9EC9FE; background:#ECF3FD;}.code pre{ font-family:"Courier New";font-size:14px;}.code pre code.note{ color:#999}.code2{border:1px solid #FEB0B0; background:#FFF1F1; margin-top:10px;}.code2 pre{ margin-left:20px; font-size:12px;}.info{ font-size:12px; color:#666666; font-family:Verdana; margin:20px 0 50px 0;}.info p{ margin:0; padding:0; line-height:22px; text-indent:40px;}h2.title{ margin:0; padding:0; margin-top:50px; font-size:18px; font-family:"微软雅⿊",Verdana;}h2.title span.titleInfo{ font-size:12px; color:#333; margin-left:10px;font-family:Verdana;}h3.title{ font-size:16px; font-family:"微软雅⿊",Verdana;}.importInfo{ font-family:Verdana; font-size:14px;}</style></head><body><li><h3><a href="demo2.html">⾃定义切换参数效果</a></h3></li></ol><div id="KinSlideshow" style="visibility:hidden;"><a target="_blank"><img src="images/11.png" alt="" width="600" height="300" /></a><a target="_blank"><img src="images/23.png" alt="" width="600" height="300" /></a><a target="_blank"><img src="images/22.png" alt="" width="600" height="300" /></a><a target="_blank"><img src="images/5.jpg" alt="" width="600" height="300" /></a><a target="_blank"><img src="images/4.jpg" alt="" width="600" height="300" /></a></div></body></html>希望本⽂所述对⼤家的Jquery特效设计有所帮助。
jQuery实现的鼠标滑过切换图片代码实例
jQuery实现的⿏标滑过切换图⽚代码实例
jQuery实现的⿏标滑过切换图⽚代码实例:
有时候⽹页需要这样的简单效果,那就是当⿏标滑过默认图⽚的时候,能够实现图⽚的切换,可能在实际应⽤中,往往没有这么简单,不过⼤家可以⾃⾏扩展⼀下,下⾯简单介绍⼀下次效果。
html静态代码如下:
<body>
<img src="images/mayi.jpg"/>
</body>
jquery切换代码:
$(document).ready(function(){
var newImage=new Image();
var oldImage=$('img').attr('src');
newImage.src='images/new.jpg';
$('img').hover(function(){
$('img').attr('src',newImage.src);
},
function(){
$('img').attr('src',oldImage);
})
})
以上代码是实现切换的核⼼,原理很简单,就是为图⽚的注册hover事件处理函数,当⿏标悬浮的时候,修改图⽚的src属性值即可。
图片轮播(左右切换)--JS原生和jQuery实现
图⽚轮播(左右切换)--JS原⽣和jQuery实现图⽚轮播(左右切换)--js原⽣和jquery实现左右切换的做法基本步骤跟上⼀篇⽂章类似,只不过修改了⼀些特定的部分(1)⾸先是页⾯的结构部分对于我这种左右切换式1.⾸先是个外围部分(其实也就是最外边的整体wrapper)2.接着就是你设置图⽚轮播的地⽅(也就是⼀个banner吧)3.然后是⼀个图⽚组(可以⽤新的div 也可以直接使⽤ ul-->li形式)4.然后是图⽚两端的左箭头和右箭头5.然后是⼀个透明背景层,放在图⽚底部6.然后是⼀个图⽚描述info层,放在透明背景层的左下⾓(div 或 ul-->li)7.然后是⼀个按钮层,⽤来定位图⽚组的index吧,放在透明背景层的右下⾓(div 或 ul-->li)由此,可以先构造出html结构<div id="wrapper"><!-- 最外层部分 --><div id="banner"><!-- 轮播部分 --><ul class="imgList"><!-- 图⽚部分 --><li><a href="#"><img src="./img/test1.jpg" width="400px" height="200px" alt="puss in boots1"></a></li><li><a href="#"><img src="./img/test2.jpg" width="400px" height="200px" alt="puss in boots2"></a></li><li><a href="#"><img src="./img/test3.jpg" width="400px" height="200px" alt="puss in boots3"></a></li><li><a href="#"><img src="./img/test4.jpg" width="400px" height="200px" alt="puss in boots4"></a></li><li><a href="#"><img src="./img/test5.jpg" width="400px" height="200px" alt="puss in boots5"></a></li></ul><img src="./img/prev.png" width="20px" height="40px" id="prev"><img src="./img/next.png" width="20px" height="40px" id="next"><div class="bg"></div><!-- 图⽚底部背景层部分--><ul class="infoList"><!-- 图⽚左下⾓⽂字信息部分 --><li class="infoOn">puss in boots1</li><li>puss in boots2</li><li>puss in boots3</li><li>puss in boots4</li><li>puss in boots5</li></ul><ul class="indexList"><!-- 图⽚右下⾓序号部分 --><li class="indexOn">1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul></div></div>相对于之前,知识增多了两个箭头img标签(2)CSS样式部分(图⽚组的处理)跟淡⼊淡出式就不⼀样了淡⼊淡出只需要显⽰或者隐藏对应序号的图⽚就⾏了,直接通过display来设定左右切换式则是采⽤图⽚li 浮动,⽗层元素ul 总宽为总图⽚宽,并设定为有限banner宽度下隐藏超出宽度的部分然后当想切换到某序号的图⽚时,则采⽤其ul 定位 left样式设定相应属性值实现⽐如显⽰第⼀张图⽚初始定位left为0px, 要想显⽰第⼆张图⽚则需要left:-400px 处理<style type="text/css">body,div,ul,li,a,img{margin: 0;padding: 0;}ul,li{list-style: none;}a{text-decoration: none;}#wrapper{position: relative;margin: 30px auto;width: 400px;height: 200px;}#banner{position:relative;width: 400px;height: 200px;overflow: hidden;}.imgList{position:relative;width:2000px;height:200px;z-index: 10;overflow: hidden;}.imgList li{float:left;display: inline;}#prev,#next{position: absolute;top:80px;z-index: 20;cursor: pointer;opacity: 0.2;filter:alpha(opacity=20);}#prev{left: 10px;}#next{right: 10px;}#prev:hover,#next:hover{opacity: 0.5;filter:alpha(opacity=50);}.bg{position: absolute;bottom: 0;width: 400px;height: 40px;z-index:20;opacity: 0.4;filter:alpha(opacity=40);background: black;} .infoList{position: absolute;left: 10px;bottom: 10px;z-index: 30;}.infoList li{display: none;}.infoList .infoOn{display: inline;color: white;}.indexList{position: absolute;right: 10px;bottom: 5px;z-index: 30;}.indexList li{float: left;margin-right: 5px;padding: 2px 4px;border: 2px solid black;background: grey;cursor: pointer;}.indexList .indexOn{background: red;font-weight: bold;color: white;}</style>(3)页⾯基本已经构建好久可以进⾏js的处理了⼀、jQuery⽅式照常先说jq处理1.全局变量等var curIndex = 0, //当前indeximgLen = $(".imgList li").length; //图⽚总数2.⾃动切换定时器处理// 定时器⾃动变换2.5秒每次var autoChange = setInterval(function(){if(curIndex < imgLen-1){curIndex ++;}else{curIndex = 0;}//调⽤变换处理函数changeTo(curIndex);},2500);3.为左右箭头添加事件处理左箭头//左箭头滑⼊滑出事件处理$("#prev").hover(function(){//滑⼊清除定时器clearInterval(autoChange);},function(){//滑出则重置定时器autoChangeAgain();});//左箭头点击处理$("#prev").click(function(){//根据curIndex进⾏上⼀个图⽚处理curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);changeTo(curIndex);});右箭头//右箭头滑⼊滑出事件处理$("#next").hover(function(){//滑⼊清除定时器clearInterval(autoChange);},function(){//滑出则重置定时器autoChangeAgain();});//右箭头点击处理$("#next").click(function(){curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;changeTo(curIndex);});其中autoChangeAgain()就是⼀个重置定时器函数//清除定时器时候的重置定时器--封装function autoChangeAgain(){autoChange = setInterval(function(){if(curIndex < imgLen-1){curIndex ++;}else{curIndex = 0;}//调⽤变换处理函数changeTo(curIndex);},2500);}其中changeTo()就是⼀个图⽚切换的处理函数function changeTo(num){var goLeft = num * 400;$(".imgList").animate({left: "-" + goLeft + "px"},500);$(".infoList").find("li").removeClass("infoOn").eq(num).addClass("infoOn");$(".indexList").find("li").removeClass("indexOn").eq(num).addClass("indexOn");}每传⼊⼀个图⽚序号,则按理进⾏goLeft4.为右下⾓的那⼏个li 按钮绑定事件处理//对右下⾓按钮index进⾏事件绑定处理等$(".indexList").find("li").each(function(item){$(this).hover(function(){clearInterval(autoChange);changeTo(item);curIndex = item;},function(){autoChangeAgain();});});jq就是这样,简便,原⽣代码量就有些多了完整代码1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2<html xmlns="/1999/xhtml">3<head>4<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>5<title>图⽚轮播 jq(左右切换)</title>6<style type="text/css">7 body,div,ul,li,a,img{margin: 0;padding: 0;}8 ul,li{list-style: none;}9 a{text-decoration: none;}1011 #wrapper{position: relative;margin: 30px auto;width: 400px;height: 200px;}12 #banner{position:relative;width: 400px;height: 200px;overflow: hidden;}13 .imgList{position:relative;width:2000px;height:200px;z-index: 10;overflow: hidden;}14 .imgList li{float:left;display: inline;}15 #prev,16 #next{position: absolute;top:80px;z-index: 20;cursor: pointer;opacity: 0.2;filter:alpha(opacity=20);}17 #prev{left: 10px;}18 #next{right: 10px;}19 #prev:hover,20 #next:hover{opacity: 0.5;filter:alpha(opacity=50);}21 .bg{position: absolute;bottom: 0;width: 400px;height: 40px;z-index:20;opacity: 0.4;filter:alpha(opacity=40);background: black;}22 .infoList{position: absolute;left: 10px;bottom: 10px;z-index: 30;}23 .infoList li{display: none;}24 .infoList .infoOn{display: inline;color: white;}25 .indexList{position: absolute;right: 10px;bottom: 5px;z-index: 30;}26 .indexList li{float: left;margin-right: 5px;padding: 2px 4px;border: 2px solid black;background: grey;cursor: pointer;}27 .indexList .indexOn{background: red;font-weight: bold;color: white;}28</style>29</head>30<body>31<div id="wrapper"><!-- 最外层部分 -->32<div id="banner"><!-- 轮播部分 -->33<ul class="imgList"><!-- 图⽚部分 -->34<li><a href="#"><img src="./img/test1.jpg" width="400px" height="200px" alt="puss in boots1"></a></li>35<li><a href="#"><img src="./img/test2.jpg" width="400px" height="200px" alt="puss in boots2"></a></li>36<li><a href="#"><img src="./img/test3.jpg" width="400px" height="200px" alt="puss in boots3"></a></li>37<li><a href="#"><img src="./img/test4.jpg" width="400px" height="200px" alt="puss in boots4"></a></li>38<li><a href="#"><img src="./img/test5.jpg" width="400px" height="200px" alt="puss in boots5"></a></li>39</ul>40<img src="./img/prev.png" width="20px" height="40px" id="prev"> 41<img src="./img/next.png" width="20px" height="40px" id="next"> 42<div class="bg"></div><!-- 图⽚底部背景层部分-->43<ul class="infoList"><!-- 图⽚左下⾓⽂字信息部分 -->44<li class="infoOn">puss in boots1</li>45<li>puss in boots2</li>46<li>puss in boots3</li>47<li>puss in boots4</li>48<li>puss in boots5</li>49</ul>50<ul class="indexList"><!-- 图⽚右下⾓序号部分 -->51<li class="indexOn">1</li>52<li>2</li>53<li>3</li>54<li>4</li>55<li>5</li>56</ul>57</div>58</div>59<script type="text/javascript" src="./js/jquery.min.js"></script>60<script type="text/javascript">61var curIndex = 0, //当前index62 imgLen = $(".imgList li").length; //图⽚总数63// 定时器⾃动变换2.5秒每次64var autoChange = setInterval(function(){65if(curIndex < imgLen-1){66 curIndex ++;67 }else{68 curIndex = 0;69 }70//调⽤变换处理函数71 changeTo(curIndex);72 },2500);7374//左箭头滑⼊滑出事件处理75 $("#prev").hover(function(){76//滑⼊清除定时器77 clearInterval(autoChange);78 },function(){79//滑出则重置定时器80 autoChangeAgain();81 });82//左箭头点击处理83 $("#prev").click(function(){84//根据curIndex进⾏上⼀个图⽚处理85 curIndex = (curIndex > 0) ? (--curIndex) : (imgLen - 1);86 changeTo(curIndex);87 });8889//右箭头滑⼊滑出事件处理90 $("#next").hover(function(){91//滑⼊清除定时器92 clearInterval(autoChange);93 },function(){94//滑出则重置定时器95 autoChangeAgain();96 });97//右箭头点击处理98 $("#next").click(function(){99 curIndex = (curIndex < imgLen - 1) ? (++curIndex) : 0;100 changeTo(curIndex);101 });102103//对右下⾓按钮index进⾏事件绑定处理等104 $(".indexList").find("li").each(function(item){105 $(this).hover(function(){106 clearInterval(autoChange);107 changeTo(item);108 curIndex = item;109 },function(){110 autoChangeAgain();111 });112 });113114//清除定时器时候的重置定时器--封装115function autoChangeAgain(){116 autoChange = setInterval(function(){117if(curIndex < imgLen-1){118 curIndex ++;119 }else{120 curIndex = 0;121 }122//调⽤变换处理函数123 changeTo(curIndex);124 },2500);125 }126127function changeTo(num){128var goLeft = num * 400;129 $(".imgList").animate({left: "-" + goLeft + "px"},500);130 $(".infoList").find("li").removeClass("infoOn").eq(num).addClass("infoOn");131 $(".indexList").find("li").removeClass("indexOn").eq(num).addClass("indexOn");132 }133</script>134</body>135</html>View Code⼆、js 原⽣实现js原⽣⼤概也就是模拟jq的实现思路1.全局变量等var curIndex = 0, //当前indeximgArr = getElementsByClassName("imgList")[0].getElementsByTagName("li"), //获取图⽚组imgLen = imgArr.length,infoArr = getElementsByClassName("infoList")[0].getElementsByTagName("li"), //获取图⽚info组indexArr = getElementsByClassName("indexList")[0].getElementsByTagName("li"); //获取控制index组2.⾃动切换定时器处理// 定时器⾃动变换2.5秒每次var autoChange = setInterval(function(){if(curIndex < imgLen -1){curIndex ++;}else{curIndex = 0;}//调⽤变换处理函数changeTo(curIndex);},2500);同样的,有⼀个重置定时器的函数//清除定时器时候的重置定时器--封装function autoChangeAgain(){autoChange = setInterval(function(){if(curIndex < imgLen -1){curIndex ++;}else{curIndex = 0;}//调⽤变换处理函数changeTo(curIndex);},2500);}3.因为有⼀些class呀,所以来⼏个class函数的模拟也是需要的//通过class获取节点function getElementsByClassName(className){var classArr = [];var tags = document.getElementsByTagName('*');for(var item in tags){if(tags[item].nodeType == 1){if(tags[item].getAttribute('class') == className){classArr.push(tags[item]);}}}return classArr; //返回}// 判断obj是否有此classfunction hasClass(obj,cls){ //class位于单词边界return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));}//给 obj添加classfunction addClass(obj,cls){if(!this.hasClass(obj,cls)){obj.className += cls;}}//移除obj对应的classfunction removeClass(obj,cls){if(hasClass(obj,cls)){var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');obj.className = obj.className.replace(reg,'');}}4.要左右切换,就得模拟jq的animate-->left .我的思路就是动态地设置element.style.left 进⾏定位。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
点击/划过按钮,图片左右切换轮播效果代码最后做成的效果:
随着鼠标点击下面的1-5个按钮,上面的大图会进行左右自动切换。
Html部分:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>图片左右切换效果制作</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/style.js"></script>
</head>
<body>
<div class="banner">
<ul>
<li>
<a href="#"><img src="images/1.jpg" alt="装修效果图" width="550" height="368"></a>
</li>
<li>
<a href="#"><img src="images/2.jpg" alt="装修效果图" width="550" height="368"></a>
</li>
<li>
<a href="#"><img src="images/3.jpg" alt="装修效果图" width="550" height="368"></a>
</li>
<li>
<a href="#"><img src="images/4.jpg" alt="装修效果图" width="550" height="368"></a>
</li>
<li>
<a href="#"><img src="images/5.jpg" alt="装修效果图" width="550" height="368"></a>
</li>
</ul>
</div>
<div class="anniu">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
</body>
</html>
Css部分:
*{margin:0px;padding:0px}
li{list-style:none}
img{border:0px}
.banner{width:550px;height:368px;margin:100px auto 10px auto;overflow:hidden}
.banner ul{width:9999px}
.banner ul li{width:550px;height:368px;float:left}
.anniu{width:100px;height:16px;margin:0px auto}
.anniu ul{width:100px;height:16px}
.anniu ul li{width:16px;height:16px;text-align:center;line-height:16px;background:red;
color:white;cursor:pointer;float:left;margin-left:2px;display:inline}
.anniu ul li.on{background:black}
Js部分:
$(function(){
$(".anniu ul li").click(function(){
$(this).addClass("on").siblings().removeClass("on");
$(".banner ul").animate({marginLeft:-550*$(this).index()},"slow")
})
})
#使用方法#:按照正常的网页制作流程,在本地新建站点文件夹,将以上代码分别放入对应的文件夹中即可看到效果。
(代码可复制)。
更多web前端开发及网页制作技术尽在本文库,欢迎持续关注!。