Html、js图片轮播代码
HTML+CSS+JavaScript实现轮播图
HTML+CSS+JavaScript实现轮播图从mooc⽹站哔哩哔哩上学到的:视频BV号为BV16W41127aQ和BV1hW411y79J,up:红点⼯⼚HTML<div class="lunboContainer"><div class="lunboLeft"><!--轮播图向左移动--><img src="选择您的路径/icon-slides.png" id="leftImg"></div><div class="lunboRight"><!--轮播图向右移动--><img src="选择您的路径/icon-slides.png" id="rightImg"></div><ul id="imgList"><li class="item active"><img src="选择您的路径/1.jpg"></li><li class="item"><img src="选择您的路径/2.jpg"></li><li class="item"><img src="选择您的路径/3.jpg"></li><li class="item"><img src="选择您的路径/4.jpg"></li><li class="item"><img src="选择您的路径/5.jpg"></li></ul><ul id="pointList"><!-- 轮播图下⾯的⼩点点,点哪个点显⽰对应的图⽚--><li class="point active" data-index='0'></li><li class="point" data-index='1'></li><li class="point" data-index='2'></li><li class="point" data-index='3'></li><li class="point" data-index='4'></li></ul></div>CSS.lunboContainer{width:100%;height:400px;margin:0 auto;position:relative;}.lunboLeft{position:absolute;z-index:10;margin-left:0px;margin-top:130px;width:60px;height:100px;overflow:hidden;}.lunboLeft img{height:100px;margin-left:-120px;}.lunboRight{position:absolute;z-index:10;right:0px;margin-top:130px;width:60px;height:100px;overflow:hidden;}.lunboRight img{height:100px;margin-left:-180px;}#imgList{width:100%;height:400px;padding:0; /* padding 设置0 */margin:0; /* margin 设置0 */position:relative;}.item{position:absolute;width:100%;list-style-type: none;height:100%;float: left;opacity:0;transition:opacity 1s;}.lunboContainer ul img{width:100%;height:100%;.item.active{opacity:1;z-index:5;}#pointList{padding:0;list-style-type: none;position:absolute;right:20px;bottom:20px;z-index:10;}.point{width:8px;height:8px;border-radius:100%;background-color:rgb(10,10,10);float:left;z-index:10;margin-right:18px;border-style:solid;border-width:2px;border-color:white;cursor:pointer;}.point.active{background-color:rgba(255,255,255,0.6);}//为正在显⽰的点点设置特殊样式Javascriptvar index=0;var imgs=document.getElementsByClassName("item");//图⽚var leftImg=document.getElementById("leftImg");//向左var rightImg=document.getElementById('rightImg');//向右var points=document.getElementsByClassName("point");//⼩点点var timeOut=0;function clearActive(){for(var i=0;i<imgs.length;i++){imgs[i].className='item';points[i].className='point';}}function goNext(){clearActive();index++;index=index%imgs.length;imgs[index].className='item active';points[index].className='point active';timeOut=0;}function goPre(){clearActive();index--;if(index<0){index=imgs.length-1;}imgs[index].className='item active';points[index].className='point active';}/*当⿏标悬停在向左向右的图⽚上⽅时需要改变图⽚样式使⽤户得到相应的反馈,这⾥学习了⼩⽶商城主页轮播图的做法,只⽤⼀张图⽚,通过改变这张图⽚到边界的距离实现样式改变*/ function preHover(){leftImg.style.marginLeft="0px";}function nextHover(){rightImg.style.marginLeft="-60px";}function preHout(){leftImg.style.marginLeft="-120px";}function nextHout(){rightImg.style.marginLeft="-180px";}/*************************************************/function jmpByPoint(pointIndex){clearActive();index=pointIndex;imgs[index].className="item active";points[index].className='point active';timeOut=0;}for(var i=0;i<points.length;i++){points[i].addEventListener('click',function(){var pointIndex=this.getAttribute("data-index");jmpByPoint(pointIndex);}leftImg.addEventListener('click',function(){goPre();})leftImg.addEventListener('mouseover',function(){preHover();})leftImg.addEventListener('mouseout',function(){preHout();})rightImg.addEventListener('click',function(){goNext();})rightImg.addEventListener('mouseover',function(){nextHover();})rightImg.addEventListener('mouseout',function(){nextHout();})setInterval(function(){timeOut++;if(timeOut==10){goNext();timeOut=0;}},500)/*此⽅法使得timeOut参数每隔0.5(500ms)秒加⼀,当timeOut加到10时(即5秒)显⽰下⼀张图⽚,同时timeOut清零,使⽤timeOut参数⽽不直接使⽤setInterval(fun(),5000)函数定时的⽬的在于:假设当⽤户点击点点跳到某张图⽚时,距离到达5000毫秒只剩⼀丝丝时间,那张图⽚就马上跳⾛了,⽽⽤户可能还没来得及看清除图⽚,使⽤timeOut定时后,当⽤户通过点跳转之后,将timeOut参数清零,可实现重新计时,就不会马上跳⾛,见function jmpByPoint(pointIndex);*/。
带左右箭头图片轮播的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>。
前端开发技术之图片轮播效果实现
前端开发技术之图片轮播效果实现随着网页设计的重要性不断增加,用户对于页面的视觉体验要求也越来越高。
作为前端开发人员,我们常常需要通过各种方式来提升页面的吸引力。
其中,图片轮播效果是非常常见而且实用的一种手段。
本文将介绍几种常见的图片轮播效果实现方式。
一、基本的自动轮播效果自动轮播效果是最常见的一种图片轮播方式。
通过自动切换图片,可以展示多张图片内容,给用户提供更多的信息。
在实现自动轮播效果时,常常使用JavaScript和CSS来完成。
1. HTML结构首先,我们需要创建一个容器来显示图片内容。
在HTML代码中,可以使用`<div>`元素作为容器,并设置一个唯一的ID,如"slider"。
在容器内部,可以使用`<img>`元素来插入图片,并使用CSS来设置图片的样式。
```<div id="slider"><img src="image1.jpg" alt="Image 1"><img src="image2.jpg" alt="Image 2"><img src="image3.jpg" alt="Image 3"></div>```2. CSS样式接下来,我们需要通过CSS来设置容器和图片的样式。
首先,我们可以设置容器的宽度和高度,并设置`overflow: hidden`来隐藏超出容器的部分。
然后,可以设置每个图片的宽度和高度,并使用`position: absolute`将它们叠加在一起。
最后,我们可以通过设置`left`和`top`属性,将图片按需求进行定位。
```#slider {width: 500px;height: 300px;overflow: hidden;position: relative;}#slider img {width: 100%;height: 100%;position: absolute;left: 0;top: 0;transition: opacity 1s;}```3. JavaScript代码最后,我们需要使用JavaScript来实现图片的切换效果。
用javaScript实现轮播图效果包括自动变换,按钮控制,上一张下一张切换
⽤javaScript实现轮播图效果包括⾃动变换,按钮控制,上⼀张下⼀张切换javaScript实现轮播图效果(逆战总结)1.HTML代码<div id="wrap"><img src="images/1.jpg" alt="" class="on"><img src="images/2.jpg" alt=""><img src="images/3.jpg" alt=""><img src="images/4.jpg" alt=""><div class="btn"><span class="active"></span><span></span><span></span><span></span></div><i class="left" id="prev"></i><i class="right" id="next"></i></div>2.css代码#wrap{position: relative;width:590px;height: 470px;}#wrap img{position: absolute;top: 0;left: 0;/*display: none;*/opacity: 0;}#wrap .on{/*display: block;*/transition: 2s;opacity: 1;}.btn {position: absolute;bottom: 20px;left: 50%;margin-left: -44px;}.btn span{float: left;width: 8px;height: 8px;border:2px solid rgba(255,255,255,0.4);border-radius: 5px;margin-right: 10px;}.btn .active{background: white;cursor: pointer;}.left,.right{display: block;width: 37px;height: 53px;position: absolute;top: 50%;margin-top: -27px;cursor: pointer;}.left{background: url("images/arrow.png") left top;}.right{background: url("images/arrow.png") left -53px;right: 0;}3.js代码window.onload = function () {var oWrap = document.getElementById('wrap');var aImg = oWrap.getElementsByTagName('img');var aBtn = oWrap.getElementsByTagName('span');var oPrev = document.getElementById('prev');var oNext = document.getElementById('next');var iNow =0;function tab(){for (var i =0;i<aBtn.length;i++){//清空所有按钮选中样式以及图⽚显⽰样式aBtn[i].className='';aImg[i].className='';}aBtn[iNow].className='active';//设置当前按钮选中样式以及当前图⽚透明度aImg[iNow].className='on';}setInterval( function () {//每两秒循环变换下⼀张图⽚iNow++;if (iNow==aBtn.length)iNow=0;tab();},2000);for (var i =0;i<aBtn.length;i++){aBtn[i].index=i;//为按钮添加⾃定义属性(索引)⽬的是使图⽚和按钮相对应//按钮aBtn[i].οnmοuseοver= function () {iNow=this.index; //是按钮所控制显⽰的图⽚与左右箭头控制显⽰的图⽚相对应tab();}//下⼀个箭头oNext.onclick = function () {iNow++;if (iNow==aImg.length)iNow=0;tab();}//上⼀个箭头oPrev.onclick = function () {iNow--;if (iNow==-1)iNow=aImg.length-1;tab();}}}。
html图片轮播代码
<div style="position:relative; top:-50px; left:240px;">
<a href="javascript:show(1)"><span id="I1" style="width:18px; text-align:left; background:gray">1</span></a>
else
{document.getElementById('pic'+i).style.display='none'; document.getElementById('I'+i).style.backgroundColor='gray'axIndex)
}
for(var i=1;i<(maxIndex+1);i++){
if(i==nowIndex)
{document.getElementById('pic'+nowIndex).style.display=''; document.getElementById('I'+nowIndex).style.backgroundColor='red';}
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="butong_net_left1" valign="top" align="center">
js轮播图代码分享_
js轮播图代码分享_大家喜爱的js轮播图片效果,分享给大家。
一、要点:1.页面加载时,图片重合,叠在一起[肯定定位];2.第一张显示,其它隐蔽;3.设置下标,给下标设置颜色让它随图片移动;4.鼠标移动到图片上去,显示左右移动图标,鼠标移走,连续轮播;二、实现代码:html代码:!DOCTYPE htmlhtml xmlns="l"headmeta http-equiv="Content-Type" content="text/html; charset=utf-8"/title轮播图/titlelink href="css/LunBimg.css" rel="stylesheet" /script src="js/jquery-1.10.2.min.js"/scriptscript src="js/LunBimg.js"/script/headbodydiv id="allswapImg"div class="swapImg"img src="image/1.jpg" //div div class="swapImg"img src="image/2.jpg" //div div class="swapImg"img src="image/3.jpg" //div div class="swapImg"img src="image/4.jpg" //div div class="swapImg"img src="image/5.jpg" //div div class="swapImg"img src="image/6.jpg" //div /divdiv class="btn btnLeft"/divdiv class="btn btnRight"/divdiv id="tabs"div class="tab bg"1/divdiv class="tab"2/divdiv class="tab"3/divdiv class="tab"4/divdiv class="tab"5/divdiv class="tab"6/div/div/body/htmlcss代码:* {padding:0px;margin:0px;}.swapImg {position:absolute;}.btn {position:absolute;height:90px;width:60px;background:rgba(0,0,0,0.5);/*设置背景颜色为黑色,透亮度为50%*/color:#ffffff;text-align:center;line-height:90px;font-size:40px;top:155px;/*图片高度400/2-45*/cursor:pointer;/*display:none;*/}.btnRight {left:840px;/*图片宽度900-导航宽度60*/}#tabs {position:absolute; top:370px;margin-left:350px; }.tab {height:20px;width:20px;background:#05e9e2; line-height:20px; text-align:center; font-size:10px;float:left;color:#ffffff;margin-right:10px; border-radius:100%; cursor:pointer;}.bg {background:#00ff21; }js代码:/// reference path="_references.js" /var i = 0;//全局变量//定义一个变量用来猎取轮播的过程var time;$(function (){//1.页面加载后,找到Class等于swapImg的第一个对象,让它显示,它的兄弟元素隐蔽$(".swapImg").eq(0).show().siblings().hide();showTime();//当鼠标放到下标上显示该图片,鼠标移走连续轮播$(".tab").hover(function (){//猎取到当前鼠标所在的下标的索引i = $(this).index();show();//鼠标放上去之后,怎么停止呢?猎取到变量的过程,清除轮播,把变量传进去clearInterval(time);}, function (){showTime();});//要求四,当我点击左右切换$(".btnLeft").click(function (){//1.点击之前要停止轮播clearInterval(time);//点了之后,-1if (i == 0){i =6;}i--;show();showTime();});$(".btnRight").click(function () { //1.点击之前要停止轮播clearInterval(time);//点了之后,-1if (i == 5) {i = -1;}i++;show();showTime();});});function show() {//$("#allswapImg").hover(function ()//{// $(".btn").show();//}, function ()//{// $(".btn").hide();//});//fadeIn(300)淡入,fadeout(300)淡出,过滤时间0.3s$(".swapImg").eq(i).fadeIn(300).siblings().fadeOut( );$(".tab").eq(i).addClass("bg").siblings().removeClass("bg");}function showTime(){time = setInterval(function () {i++;if (i == 6) {//只有6张图片,所以i不能超过6,假如i等于6时,我们就让它等于第一张i = 0;}show();}, 3000);}以上就是本文的全部内容,盼望对大家的学习有所关心...。
最简单的JavaScript图片轮播代码(两种方法)
最简单的JavaScript图片轮播代码(两种方法)通过改变每个图片的opacity属性:素材图片:代码一:最简单的轮播广告代码二:第二步,布局好一个DIV,如:上一张下一张第三步,就到了写CSS的时候了.下面的CSS懂基础的人都看得懂.#croll{width:100%;height:180p;background-color:white;poition:relative;border-bottom:1polidgray;}、、这里是给整个大的DIV设定属性.#crollul{height:180p;lit-tyle:none;}、、DIV下的UL属性.#crollulli{width:100%;height:180p;margin:0p;padding:0p;dipla y:none;}、、DIV下的UL下的LI属性.注意:diplay:none;因为要将所有的li隐藏了先.当点击的时候在显示出来.。
ubl{poition:abolute;bottom:20p;left:40%;width:80p;height:20p;lin e-height:20p;tet-align:center;font-ize:16p;font-weight:bold;curor:pointer;}、、上一张按钮的属性.注意一个绝对定位.。
ubr{poition:abolute;bottom:20p;right:40%;width:80p;height:20p;line-height:20p;tet-align:center;font-ize:16p;font-weight:bold;curor:pointer;}、、下一张按钮的属性.注意一个绝对定位.。
ubr:hover{background:yellow;border-radiu:10p;}。
ubl:hover{background:yellow;border-radiu:10p;}、、以上两个hover是鼠标经过的效果.第四步,就是jquery代码了!也很简单.先将代码看一遍,你就会用了!四步轻松搞定一个简单的轮!。
两种方法实现显示多张图片的轮播
两种⽅法实现显⽰多张图⽚的轮播两种⽅法实现显⽰多张图⽚的轮播第⼀种:jQuery ⽅法.animate + 浮动排列布局1 <!DOCTYPE html>2 <html>3 <head>4 <meta charset="utf-8" />5 <title>显⽰多张图⽚的轮播</title>6 <style type="text/css">7 html,body{8 padding:0;9 margin:10px auto;10 }11 #warpper{12 width:1120px;/*⼀列展⽰⼏个的宽度 ,4*280 动态算*/13 height:150px;14 margin:0 auto;15 position:relative;16 overflow:hidden;17 border:1px solid red;18 }19 #inner{20/*元素个数*280(div宽度) 动态算 1120px[4个元素] 1440px[5个元素] 1680px[6个元素] 2240px[8个元素]21⽤JS动态设置改值22*/23 width:2520px;24 position:absolute;25 }26 #inner div{27 width:270px;28 height:150px;29/*使⽤浮动元素排列*/30float:left;31 margin:0 5px;32 }33 #optrbtn{34 text-align:center;35 font-size:20px;36 }37 #optrbtn span {38 display:inline-table;39 border:1px solid red;40 width:50px;41 cursor:pointer;42 margin-right:5px;43 }44 #inner div img{45 width: 100%;46 height: 100%;47 }48 </style>4950 <script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>51 <script type="text/javascript">5253 $(function(){5455var curNum=0;56var count=$("#inner").children('div').length;//85758//左滑动59 $(".btnleft").click(function(){60//如果到了最后⼀个元素,停⽌61if((count-curNum)<=4){62return false;63 }64 curNum+=1;65 $("#inner").animate({66// 每次点击都从左边裁剪280像素67 left:'-=280'68 },500);69 });7071//右滑动72 $(".btnright").click(function(){73//如果到了第⼀个元素,停⽌74if(curNum<=0){75return false;76 }77 curNum-=1;78 $("#inner").animate({79// 每次点击都从左边补充280像素80 left:'+=280'81 },500);82 });83 });8485 </script>86 </head>8788 <body>8990 <div id="main">91 <div id="warpper">92 <div id="inner">93 <div><img src="../img/sucai/full1.jpg"/></div>94 <div><img src="../img/sucai/full2.jpg"/></div>95 <div><img src="../img/sucai/full3.jpg"/></div>96 <div><img src="../img/sucai/full4.jpg"/></div>97 <div><img src="../img/sucai/full5.jpg"/></div>98 <div><img src="../img/sucai/full6.jpg"/></div>99 <div><img src="../img/sucai/full7.jpg"/></div>100 <div><img src="../img/sucai/full8.jpg"/></div>101 <div><img src="../img/sucai/full9.jpg"/></div>102 </div>103 </div>104 </div>105106 <br />107 <br />108109 <div id="optrbtn">110 <span class="btnleft">«</span>111 <span class="btnright">»</span>112 </div>113114 </body>115 </html>第⼆种:负边距 + flex排列布局1 <!DOCTYPE html>2 <html lang="en">3 <head>4 <meta charset="UTF-8">5 <title>显⽰多张图⽚的轮播</title>6 <style type="text/css" rel="stylesheet">7 *{8 margin: 0;9 padding: 0;10 }11 #main{12 width: 100%;13 height: 125px;14 display: flex;15 margin-top: 120px;16 }17 #d1{18 width: 1162px;19 height: 125px;20 display: flex;21 overflow:hidden;22 }23 #d11{24 width: 64px;25 height: 125px;26 line-height: 125px;27 text-align: center;28 font-size: 28px;29 cursor: pointer;30 z-index: 2;31 background: #FFFFFF;32 }33 #d13{34 width: 64px;35 height: 125px;36 line-height: 125px;37 text-align: center;38 font-size: 28px;39 cursor: pointer;40 z-index: 2;41 background: #FFFFFF;42 }43 #d12{44/*元素个数*207(div宽度) 动态算 828px[4个元素] 1035px[5个元素] 1242px[6个元素] 2070px[10个元素] 45⽤JS动态设置改值46*/47/*先预设⾜够放10张图⽚的宽度*/48 width: 2070px;49 height: 125px;50/*使⽤弹性布局排列*/51 display: flex;52 transition:all 0.3s linear 0s;53/*再将多出来的5张图⽚的宽度⽤负边距隐藏起来*/54 margin-right: -1035px;55 }56 #d12 .d120{57 width: 207px;58 height: 125px;59 z-index: 1;60 }61 .ddd{62 width: 170px;63 height: 80px;64 margin-left: 18px;65 margin-top: 23px;66 }67 .ddd img{68 width: 100%;69 height: 100%;70 }71 </style>72 </head>73 <body>7475 <div id="main">76 <div style="width: 102px;height:125px;background: #FFFFFF;z-index: 3;"></div>77 <div id="d1">78 <div id="d11">《</div>79 <div id="d12">80 <div class="d121 d120"><div class="ddd"><img src="../img/sucai/full1.jpg"></div></div>81 <div class="d122 d120"><div class="ddd"><img src="../img/sucai/full2.jpg"></div></div>82 <div class="d121 d120"><div class="ddd"><img src="../img/sucai/full3.jpg"></div></div>83 <div class="d122 d120"><div class="ddd"><img src="../img/sucai/full4.jpg"></div></div>84 <div class="d121 d120"><div class="ddd"><img src="../img/sucai/full5.jpg"></div></div>85 <div class="d122 d120"><div class="ddd"><img src="../img/sucai/full6.jpg"></div></div>86 <div class="d121 d120"><div class="ddd"><img src="../img/sucai/full7.jpg"></div></div>87 <div class="d122 d120"><div class="ddd"><img src="../img/sucai/full8.jpg"></div></div>88 <div class="d121 d120"><div class="ddd"><img src="../img/sucai/full9.jpg"></div></div>89 <div class="d122 d120"><div class="ddd"><img src="../img/sucai/full1.jpg"></div></div>90 </div>91 <div id="d13">》</div>92 </div>93 <div style="width: 102px;height:125px;background: #FFFFFF;z-index: 3;"></div>94 </div>9596 </body>97 <script type="text/javascript">9899 let $ = function(id){100return document.getElementById(id);101 };102103//移动的宽度104var moveLength = 0;105106// 右移107 $("d13").addEventListener("click",function () {108 moveLength +=207;109// 判断移动的宽度有没有超出5张图⽚的宽度,如果超出,则减掉⼀张图⽚的宽度110if (moveLength<=1035){111 $("d12").style = "transform:translateX(-"+moveLength+"px);"112 }else if(moveLength>1035){113 moveLength -= 207;114 }115 });116117// 定时器,每个3秒移动⼀次118 setInterval(function () {119 moveLength +=207;120if (moveLength<=1035){//是否移动到尽头121 $("d12").style = "transform:translateX(-"+moveLength+"px);"122 }else if(moveLength>1035){//是否移动到尽头123 moveLength -= 1242;124 $("d12").style = "transform:translateX(-"+moveLength+"px);"125 }126 },3000);127128// 左移129 $("d11").addEventListener("click",function () {130// ⾸先判断图⽚是不是移动过了,是否移到尽头,然后再做调整移动的宽度131if (moveLength===1035){//是否移到了尽头132 $("d12").style = "transform:translateX(-"+moveLength+"px);";133 moveLength -=207;134 $("d12").style = "transform:translateX(-"+moveLength+"px);";135 }else if(moveLength>0&&moveLength<1035){//是否在原点与尽头之间136 moveLength -=207;137 $("d12").style = "transform:translateX(-"+moveLength+"px);";138 }else if(moveLength===0){//是否回到原点139 moveLength -= 207;140 moveLength += 207;141 }142 })143144 </script>145 </html>其实⽅法并不⽌两种,这⾥只⽤两种简单的案例来实现⽽已,你也可以举⼀反三⽤多种⽅法实现。
使用html+js+css实现页面轮播图效果(实例讲解)
使⽤html+js+css实现页⾯轮播图效果(实例讲解)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"><link rel="stylesheet" href="carousel.css" rel="external nofollow" ><title>轮播图效果</title></head><body><section id="main"><div id="picture"></div><!-- 添加图中按钮图⽚轮播在js中⼤致成型后再写最好--><div id="dot"><span></span><span></span><span></span><span></span><span></span></div><!-- 添加切换按钮 --><div id="an"><div class="left"><</div><div class="right">></div></div></section><script src="jquery.js"></script><script src="carousel.js"></script></body>css页⾯ carousel.css#main{width: 655px;height: 361px;position: relative;}#picture{width:100%;height: 100%;}#picture img{width:100%;height: 100%;display: none;}#picture img:nth-child(1){display: inline-block;}/* 设置圆点的样式 */#dot span{display: inline-block;width:25px;height: 25px;border-radius: 50%;background-color: gray;margin-left: 10px;opacity: 0.6}#dot{position: absolute;right: 40px;bottom: 30px;}/* 设置页⾯刚加载的圆点初始状态 1 第⼀个圆点放⼤1.2倍 2、颜⾊变成蓝⾊ */#dot :nth-of-type(1){transform: scale(1.2);background-color: blue;}.left ,.right{width: 40px;height: 40px;border-radius: 50%;font-size: 30px;color: white;position: absolute;bottom: calc((100% - 40px)/2);text-align: center;}.left{left: 15px;}.right{right: 15px;}.left:hover ,.right:hover{background-color: white;color:red;}js页⾯ carousel.jsfor(var i=1; i<6;i++){$('#picture').append("<img src='./../images/"+i+".jpg' >");}//给图⽚转化设置定时函数var index=0;var timer;function changeImageDot(){$('#picture img:nth-child('+(index+1)+')').css({display:'block',}).siblings().css({display:'none',});//设置随图⽚切换,对应的圆点样式发⽣变化// index+1 是因为索引是从0开始⽽ nth-child(i) 中的i是从1 开始的$('#dot span:nth-child('+(index+1)+')').css({transform: 'scale(1.2)','background-color': 'blue',}).siblings().css({transform: 'scale(1)','background-color':'gray',});}function produceTime(){timer=setInterval(function(){index++;if(index==5)index=0;changeImageDot();},2000);}produceTime();//⿏标悬浮在圆点上,圆点和图⽚的变化$('#dot span').mouseenter(function(){index=$(this).index();changeImageDot();clearInterval(timer);produceTime();});//缺点:点击切换按钮后,图⽚切换后,会⽴即切换到下⼀个图⽚,需要设置清除计时器后再新建⼀个计时器$('.left').click(function(){index--;if(index==-1)index=4;changeImageDot();clearInterval(timer);produceTime();});$('.right').click(function(){index++;if(index==5)index=0;changeImageDot();clearInterval(timer);produceTime();以上这篇使⽤html+js+css 实现页⾯轮播图效果(实例讲解)就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
网页5张图片轮播代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="/1999/xhtml"><head><title>pic player</title><script type="text/javascript" src="/jslib/jquery/jquery-1.2.6.js"></script> <script type="text/javascript" src="/jslib/jquery/tween.js"></script></head><style type="text/css">img{border:0;}</style><body><div id="picplayer" style="position:relative;overflow:hidden;width:300px;height:300px;clear:none;border:solid 1px #ccc;">there is a pic-player</div><script>var p = $('#picplayer');var pics1 = [{url:'/online/picPlayer/1.jpg',link:'',time:5000},{url: '/online/picPlayer/2.jpg',link:'',time:4000},{url:'http :///online/picPlayer/3.jpg',link:'',time:6000},{url:'http://im/online/picPlayer/2.jpg',link:'',time:6000},{url:'http://img.jb/online/picPlayer/1.jpg',link:'',time:6000}];initPicPlayer(pics1,p.css('width').split('px')[0],p.css('height').split('px')[0]);////function initPicPlayer(pics,w,h){//选中的图片var selectedItem;//选中的按钮var selectedBtn;//自动播放的idvar playID;//选中图片的索引var selectedIndex;//容器var p = $('#picplayer');p.text('');p.append('<div id="piccontent"></div>');var c = $('#piccontent');for(var i=0;i<pics.length;i++){//添加图片到容器中c.append('<a href="'+pics[i].link+'" target="_blank"><img id="picitem'+i+'" style="display: none;z-index:'+i+'" src="'+pics[i].url+'" /></a>');}//按钮容器,绝对定位在右下角p.append('<div id="picbtnHolder" style="position:absolute;top:'+(h-25)+'px;width:'+w+'px;height:20px;z-index:10000;"></div >');//var btnHolder = $('#picbtnHolder');btnHolder.append('<div id="picbtns" style="float:right; padding-right:1px;"></div>');var btns = $('#picbtns');//for(var i=0;i<pics.length;i++){//增加图片对应的按钮btns.append('<span id="picbtn'+i+'" style="cursor:pointer; border:solid 1px #ccc;background-color:#eee; display:inline-block;"> '+(i+1)+' </span> ');$('#picbtn'+i).data('index',i);$('#picbtn'+i).click(function(event){if(selectedItem.attr('src') == $('#picitem'+$(this).data('index')).attr('src')){return;}setSelectedItem($(this).data('index'));});}btns.append(' ');///setSelectedItem(0);//显示指定的图片indexfunction setSelectedItem(index){selectedIndex = index;clearInterval(playID);//alert(index);if(selectedItem)selectedItem.fadeOut('fast');selectedItem = $('#picitem'+index);selectedItem.fadeIn('slow');//if(selectedBtn){selectedBtn.css('backgroundColor','#eee'); selectedBtn.css('color','#000');}selectedBtn = $('#picbtn'+index); selectedBtn.css('backgroundColor','#000'); selectedBtn.css('color','#fff');//自动播放playID = setInterval(function(){var index = selectedIndex+1;if(index > pics.length-1)index=0; setSelectedItem(index);},pics[index].time);}}</script></body></html>黄色的地方修改显示大小和显示的图片如增加图片可以通过增加var pics1 后面的内容。
js淡入淡出的图片轮播效果代码分享
js淡⼊淡出的图⽚轮播效果代码分享本⽂实例讲述了淡⼊淡出的js图⽚轮播效果代码。
分享给⼤家供⼤家参考。
具体如下:运⾏效果图:---------------------------------------------⼩提⽰:浏览器中如果不能正常运⾏,可以尝试切换浏览模式。
为⼤家分享的js图⽚轮播效果代码如下<html><head><title>js图⽚轮播效果代码</title><style type="text/css">table img:hover{cursor:pointer;}</style></head><body><div align="center"><SCRIPT>var roll_image = new Array;var image_link = new Array;var small_img = new Array;roll_image[0] = 'images/01.jpg';image_link[0] = '';small_img[0] = 'images/main_flash_button1_on.gif';roll_image[1] = 'images/02.jpg';image_link[1] = '';small_img[1] = 'images/main_flash_button2_on.gif';roll_image[2] = 'images/03.jpg';image_link[2] = '';small_img[2] = 'images/main_flash_button3_on.gif';roll_image[3] = 'images/04.jpg';image_link[3] = '';small_img[3] = 'images/main_flash_button4_on.gif';roll_image[4] = 'images/05.jpg';image_link[4] = '';small_img[4] = 'images/main_flash_button5_on.gif';roll_image[5] = 'images/06.jpg';image_link[5] = '';small_img[5] = 'images/main_flash_button6_on.gif';roll_image[6] = 'images/07.jpg';image_link[6] = '';small_img[6] = 'images/main_flash_button7_on.gif';var cliImg = '';var cliImgSrc = '';var imgNo = Math.round(Math.random() * 7);var interval = 3000;var setTime = '';function click_simg(ci, no){var pImg = document.all.bigimg;var pLink = document.all.imglink;if(cliImg == '') {cliImg = ci;cliImgSrc = ci.src;ci.src = small_img[no];imgNo=no;pImg.src =roll_image[no];pLink.href = image_link[no];} else if(cliImg != ci) {cliImg.src = cliImgSrc;cliImg = ci;cliImgSrc = ci.src;ci.src = small_img[no];imgNo=no;pImg.src =roll_image[no];pLink.href = image_link[no];}clearTimeout(setTime);setTime=setTimeout("rotate()",interval);}function rotate(){imgNo = (imgNo >= 6) ? 0 : imgNo+1;var ci = eval('document.all.num_img'+imgNo);document.all.bigimg.filters.blendTrans.apply();document.all.imglink.href=image_link[imgNo];document.all.bigimg.src=roll_image[imgNo];document.all.bigimg.filters.blendTrans.play();if(cliImg == '') {cliImg = ci;cliImgSrc = ci.src;ci.src = small_img[imgNo];} else if(cliImg != ci) {cliImg.src = cliImgSrc;cliImg = ci;cliImgSrc = ci.src;ci.src = small_img[imgNo];}setTime=setTimeout("rotate()",interval);}//--></SCRIPT><TABLE cellSpacing=0 cellPadding=0 width=520 border=0><TBODY><TR><TD height=338><A onfocus=this.blur() href="#" name=imglink><IMG style="FILTER: blendTrans(duration=1)" height=338 src="images/01.jpg" width=520 border=0 name=bigimg></A> </TD></TR><TR><TD height=27><TABLE cellSpacing=0 cellPadding=0 width="100%" border=0><TBODY><TR><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 0);" height=15src="images/main_flash_button1.gif" width=61border=0 name=num_img0></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 1);" height=15src="images/main_flash_button2.gif" width=61border=0 name=num_img1></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 2);" height=15src="images/main_flash_button3.gif" width=61border=0 name=num_img2></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 3);" height=15src="images/main_flash_button4.gif" width=61border=0 name=num_img3></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 4);" height=15src="images/main_flash_button5.gif" width=61border=0 name=num_img4></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 5);" height=15src="images/main_flash_button6.gif" width=61border=0 name=num_img5></TD><TD width=3></TD><TD width=61><IMG style="CURSOR: hand"onclick="click_simg(this, 6);" height=15src="images/main_flash_button7.gif" width=61border=0 name=num_img6></TD><TDwidth=72></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><p><SCRIPT>rotate();</SCRIPT><br></div></BODY></HTML>精彩专题分享:以上就是为⼤家分享的js图⽚轮播效果代码,希望⼤家可以喜欢。
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.⽼三步。
JavaScript图片轮播代码分享
JavaScript图⽚轮播代码分享本⽂为⼤家分享的JavaScript图⽚轮播代码如下<!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><script src="/js/jquery/1.9.1/jquery-1.9.1.min.js"></script><style>.img-div img{display:none;}</style></head><body><div class="slide" id="slide"><div class="img-div"><img src="model.jpg" /><img src="model2.jpg" /><img src="model.jpg" /><img src="model2.jpg" /><img src="model.jpg" /></div><div class="slide-btn"><a href="#" class="hover">1</a><a href="#" class="hover">2</a><a href="#" class="hover">3</a><a href="#" class="hover">4</a><a href="#" class="hover">5</a></div></div><script type="text/javascript">var zBase={$id:function(id){return document.getElementById(id);},$tagName:function(tagName,obj){return ((obj?obj:document).getElementsByTagName(tagName));},$c:function(clsN,obj){var tag=this.$tagName('*'),reg=new RegExp('(^|\\s)'+clsN+'(\\s|$)'),arr=[];for(var i=0;i<tag.length;i++){if(reg.test(tag[i].className)){arr.push(tag[i]);}}return arr;},$add:function(obj,clsN){var reg=new RegExp('(^|\\s)'+clsN+'(\\s|$)');if(!reg.test(obj.className)){obj.className+=' '+clsN;}},$remove:function(obj,clsN){var cla=obj.className;var reg='/|\\s*'+clsN+'\\b/g';obj.className=cla?cla.replace(eval(reg),''):'';},css:function(obj,attr,value){if(value){obj.style[attr]=value;}else{return typeof window.getComputedStyle!="undefined"?window.getComputedStyle(obj,null)[attr]:obj.currentStyle[attr]; }},easing:{liner:function(t,b,c,d){return c*t/d+b},easeOut:function(t,b,c,d){return -c*((t=t/d-1)*t*t*t-1)+b}},config:{index:0,auto:true,direct:'left'},init:function(){this.slide=this.$id('slide');this.img_div=this.$c('img-div')[0];this.slide_btn=this.$tagName('a',this.$c('slide-btn')[0]);this.img_arr=this.$tagName('img',this.img_div);if(this.config.auto){this.play();}this.hover();},animate:function(obj,attr,val){var d=1000;if(obj[attr+"timer"]){clearInterval(obj[attr+"timer"])};var start= parseInt(zBase.css(obj,attr));var space=val-start,st=(new Date().getTime()),m=space>0?'cell':'floor';obj[attr+'timer']=setInterval(function(){var t=(new Date().getTime()-st);if(t<d){zBase.css(obj,attr,Math[m](zBase.easing["easeOut"](t,start,space,d))+"px");}else{clearInterval(obj[attr+'timer']);zBase.css(obj,attr,top+space+"px");}},20);},hover:function(){for(var i=0;i<this.slide_btn.length;i++){this.slide_btn[i].index=i;this.slide_btn[i].onmouseover=function(){if(zBase.slide.timer){clearInterval(zBase.slide.timer);}zBase.config.index=this.index;//console.log(this.slide_btn);for(var j=0;j<zBase.slide_btn.length;j++){zBase.$remove(zBase.slide_btn[j],'hover');}zBase.$add(zBase.slide_btn[zBase.config.index],'hover');zBase.animate(zBase.img_div,zBase.config.direct,-zBase.config.index*1000); }this.slide_btn[i].onmouseout=function(){zBase.play();}}},play:function(){this.slide.timer = setInterval(function(){var tags=zBase.$tagName('img',this.img_div);//zBase.$tagName('img',this.img_div)[zBase.config.index].style.display="block"; for(var i =0;i<tags.length;i++){if(zBase.config.index==i){zBase.$tagName('img',this.img_div)[zBase.config.index].style.display="block"; }else{zBase.$tagName('img',this.img_div)[i].style.display="none";}}zBase.config.index++;if(zBase.config.index>=zBase.img_arr.length) zBase.config.index=0;zBase.animate(zBase.img_div,zBase.config.direct,-zBase.config.index*500);for(var j=0;j<zBase.slide_btn.length;j++){zBase.$remove(zBase.slide_btn[j],'hover') ;}zBase.$add(zBase.slide_btn[zBase.config.index],'hover');},3000)}}zBase.init();</script></body></html>以上就是为⼤家分享的JavaScript图⽚轮播代码,希望⼤家可以喜欢。
Html网页图片滚动代码
Html网页图片滚动代码<!--下面是向上滚动代码--><div id=butong_net_top style=overflow:hidden;height:100;width:90;><div id=butong_net_top1><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"></div><div id=butong_net_top2></div></div><script>var speed=30butong_net_top2.innerHTML=butong_net_top1.innerHTML //克隆butong_net_top1为butong_net_top2function Marquee1(){//当滚动至butong_net_top1与butong_net_top2交界时if(butong_net_top2.offsetTop-butong_net_top.scrollTop<=0)&<60;&<60;butong_net_top.scrollTop-=butong_net_top1.offsetHeight //butong_net_top跳到最顶端else{butong_net_top.scrollTop++;}}var MyMar1=setInterval(Marquee1,speed)//设置定时器//鼠标移上时清除定时器达到滚动停止的目的butong_net_top.onmouseover=function() {clearInterval(MyMar1)}//鼠标移开时重设定时器butong_net_top.onmouseout=function(){MyMar1=setInterval(Marquee1,speed)}</script><!--向上滚动代码结束--><br><!--下面是向下滚动代码--><div id=butong_net_bottom style=overflow:hidden;height:100;width:90;><div id=butong_net_bottom1><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"><img src="插入需要滚动的图片"></div><div id=butong_net_bottom2></div></div><script>var speed=30butong_net_bottom2.innerHTML=butong_net_bottom1.innerHTMLbutong_net_bottom.scrollTop=butong_net_bottom.scrollHeightfunction Marquee2(){if(butong_net_bottom1.offsetTop-butong_net_bottom.scrollTop>=0)butong_net_bottom.scrollTop+=butong_net_bottom2.offsetHeightelse{butong_net_bottom.scrollTop--}}var MyMar2=setInterval(Marquee2,speed)butong_net_bottom.onmouseover=function() {clearInterval(MyMar2)}butong_net_bottom.onmouseout=function() {MyMar2=setInterval(Marquee2,speed)} </script><!--向下滚动代码结束--><br><!--下面是向左滚动代码--><div id="butong_net_left" style="overflow:hidden;width:500px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td id="butong_net_left1" valign="top" align="center"><table cellpadding="2" cellspacing="0" border="0"><tr align="center"><td><img src="<img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td></tr></table></td><td id="butong_net_left2" valign="top"></td></tr></table></div><script>var speed=30//速度数值越大速度越慢butong_net_left2.innerHTML=butong_net_left1.innerHTMLfunction Marquee3(){if(butong_net_left2.offsetWidth-butong_net_left.scrollLeft<=0)butong_net_left.scrollLeft-=butong_net_left1.offsetWidthelse{butong_net_left.scrollLeft++}}var MyMar3=setInterval(Marquee3,speed)butong_net_left.onmouseover=function() {clearInterval(MyMar3)}butong_net_left.onmouseout=function() {MyMar3=setInterval(Marquee3,speed)} </script><!--向左滚动代码结束--><br><!--下面是向右滚动代码--><div id="butong_net_right" style="overflow:hidden;width:500px;"><table cellpadding="0" cellspacing="0" border="0"><tr><td id="butong_net_right1" valign="top" align="center"><table cellpadding="2" cellspacing="0" border="0"><tr align="center"><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td><td><img src="插入需要滚动的图片"></td></tr></table></td><td id="butong_net_right2" valign="top"></td></tr></table></div><script>var speed=30//速度数值越大速度越慢butong_net_right2.innerHTML=butong_net_right1.innerHTMLfunction Marquee4(){if(butong_net_right.scrollLeft<=0)butong_net_right.scrollLeft+=butong_net_right2.offsetWidthelse{butong_net_right.scrollLeft--}}var MyMar4=setInterval(Marquee4,speed)butong_net_right.onmouseover=function() {clearInterval(MyMar4)}butong_net_right.onmouseout=function() {MyMar4=setInterval(Marquee4,speed)} </script><!--向右滚动代码结束-->。
原生js轮播图完整代码(css+html+js)
原⽣js轮播图完整代码(css+html+js) 1<style>2 *{3 padding: 0;4 margin: 0;5 }6 .container{7 width: 600px;8 height: 320px;9 box-shadow: 0px 0px 33px 11px #25dbc7;10 margin-top: 90px;11 margin-left: 300px;12 }13 .picShow{14 width: 600px;15 height: 370px;16 overflow: hidden;17 position: relative;18 }19 .picShow img{20 display: block;21 float: left;22 }23 #pic{24 width: 2400px;25 }26 #pic>img{27 width: 600px;28 }29 .picShow>img{30 position: absolute;31 top:150px;32 opacity: 0.7;33 cursor: pointer;34 }35 .picShow>img:nth-child(2){36 left:0;37 }38 .picShow>img:nth-child(3){39 right:0;40 }41 #list{42 margin-left: 246px;43 }44 #list li{45 list-style: none;46 float: left;47 }48 #list a{49 display: block;50 width: 20px;51 height: 20px;52 font-size: 20px;53 text-align: center;54 text-decoration: none;55 color: #000;56 border-radius: 50%;57 border: 1px solid #ccc;58 background-color: #1270d8;59 opacity: 0.5;60 margin-left: 5px;61 }62 #list .active{63 background-color: red;64 }65</style>66</head>67<body>68<div class="container">69<div class="picShow">70<div id="pic">71<img src="images/1.jpg" alt="">72<img src="images/2.jpg" alt="">73<img src="images/3.jpg" alt="">74<img src="images/4.jpg" alt="">75</div>76<img id="prev" src="images/slider-prev.png" alt="">77<img id="next" src="images/slider-next.png" alt="">78<ul id="list">79<li><a class="active" href="#"></a></li>80<li><a href="#"></a></li>81<li><a href="#"></a></li>82<li><a href="#"></a></li>83</ul>84</div>85</div>86<script>87var num = 0;88function G(id){89return document.getElementById(id)90 }91var arrA = G("list").getElementsByTagName('a')92 G("next").onclick = function(){93if(num<3){94 num = num + 1;95 }else{96 num = 0;97 }98 console.log(num)99var ml = num * -600 + "px";100 G("pic").style.marginLeft = ml;101for(var i = 0;i < arrA.length;i++){102 arrA[i].style.backgroundColor = "#1270d8";103 }104 arrA[num].style.backgroundColor = "red";105 }106 G("prev").onclick = function(){107if(num>0){108 num = num - 1;109 }else{110 num = 3;111 }112 console.log(num);113var ml1 = num * -600 + "px";114 G("pic").style.marginLeft = ml1;115for(var i = 0;i < arrA.length;i++){116 arrA[i].style.backgroundColor = "#1270d8";117 }118 arrA[num].style.backgroundColor = "red";119 }120for(var i = 0;i < arrA.length;i++){121 arrA[i].index = i;122 arrA[i].onclick = function(){123var ind = this.index;124 num = ind;125var ml3 = num * -600 + "px";126 G("pic").style.marginLeft = ml3;127for(var i = 0;i < arrA.length;i++){128 arrA[i].style.backgroundColor = "#1270d8";129 }130 arrA[num].style.backgroundColor = "red";131 }132 }133function lunbo(){134if(num<3){135 num+=1;136 }else{137 num=0;138 }139var ml4 = num * -600 + "px";140 G("pic").style.marginLeft = ml4;141 console.log(ml4)142for(var i = 0;i < arrA.length;i++){143 arrA[i].style.backgroundColor = "#1270d8";144 }145 arrA[num].style.backgroundColor = "red";146 }147var stop = setInterval(lunbo,2000);148 G("pic").onmouseenter = function(){149 clearInterval(stop)150 }151 G("pic").onmouseleave = function(){152 stop = setInterval(lunbo,2000)153 }154</script>思路:布局⾸先写⼀个⼀张图⽚宽度的盒⼦,然后⾥⾯套⼀个盒⼦,盒⼦⾥⾯放上图⽚,两边左右切换的图标可以定位上去,然后说⼀下js 代码的思路先看⼀下需要拿到什么数,然后根据规律定义⼀个变量num=0,⽤if语句可以拿到想要的数。
超简单版轮播图片代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>生活有诀窍</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"/><script type="text/javascript">var maxWidth=600;var curTimer=null;var imageCount=4;function play(index){var targetLeft=-((index-1)*maxWidth);if(curTimer){clearTimeout(curTimer);curTimer=null;}trunLeft(targetLeft);for(var i=1;i<=imageCount;i++){document.getElementById("play"+i).style.backgroundColor="#a5a5a5";}document.getElementById("play"+index).style.backgroundColor="#e1e1e1";}function trunLeft(targetLeft){var curLeft=parseInt(getStyle(document.getElementById("imageUL"), "left"));var offset=Math.abs(Math.abs(curLeft)-Math.abs(targetLeft)); //此处可以采用Tween.js缓动来实现更多的效果if(offset>30){offset=30;}if(curLeft>targetLeft){curLeft-=offset;}else if(curLeft<targetLeft){curLeft+=offset;}else{if(curTimer){clearTimeout(curTimer);curTimer=null;}return;}document.getElementById("imageUL").style.left=curLeft+"px";curTimer=setTimeout("trunLeft("+targetLeft+")",10);}function getStyle(elem,name){var elem = (!elem) ? alert("ERROR: It is short of getStyle==>elem!") : elem;var name = (!name) ? alert("ERROR: It is short of getStyle==>name!") : name.toString();if((!elem) && (!name)){return false;}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></head><body>本代码由<a href="" target="_blank">生活有诀窍网站()</a>QQ:2450882851提供,仅供学习使用,请务必写明来源<div style="width:600px;"><div style="position:relative;width:600px;height:200px;overflow: hidden;"><ul id="imageUL" style="position: absolute;width:2400px;margin:0px;padding:0px;left:0px;"><li style="float:left;list-style:none;width: 600px;height: 200px;"><a href="" target="_blank"><img src="image/1.jpg" style="border: none;"/></a></li><li style="float:left;list-style:none;width: 600px;height: 200px;"><a href="" target="_blank"><img src="image/2.jpg" style="border: none;"/></a></li><li style="float:left;list-style:none;width: 600px;height: 200px;"><a href="" target="_blank"><img src="image/m.jpg" style="border: none;"/></a></li><li style="float:left;list-style:none;width: 600px;height: 200px;"><a href="" target="_blank"><img src="image/3.jpg" style="border: none;"/></a></li></ul></div><div style="position:relative;top:-20px;float:right;"><ul style="margin:0px;padding:0px;left:0px;"><li id="play1" style="background-color:#e1e1e1;float:left;list-style: none;margin-left:5px;padding:1px 5px;cursor: pointer;"><span onmouseover="javascript:play(1)">1</span></li><li id="play2" style="background-color:#a5a5a5;float:left;list-style: none;margin-left:5px;padding:1px 5px;cursor: pointer;"><span onmouseover="javascript:play(2)">2</span></li><li id="play3" style="background-color:#a5a5a5;float:left;list-style: none;margin-left:5px;padding:1px 5px;cursor: pointer;"><span onmouseover="javascript:play(3)">3</span></li><li id="play4" style="background-color:#a5a5a5;float:left;list-style: none;margin-left:5px;padding:1px 5px;cursor: pointer;"><span onmouseover="javascript:play(4)">4</span></li></ul></div></div></body></html>。
HTML+CSS+JS实现堆叠轮播效果的示例代码
HTML+CSS+JS实现堆叠轮播效果的⽰例代码效果:轮播图在向⼀个⽅向移动的同时,要对其每⼀个图⽚的⼤⼩,位置,透明度以及层级进⾏改变原理:轮播图向左移动时将第⼀个⼦元素剪切到末尾,轮播图向右移动时,将末尾⼦元素剪切到最前⾯,以此实现⽆缝轮播效果,再因为li剪切后,下⼀个li会补上(例如第⼀个⼦元素被剪切到最后时,第⼆个字元素会补上成为第⼀个⼦元素),所以li下标不变,以次来修改每⼀个位置的li的属性(⼤⼩,位置,透明度,层级)HTML代码:<body><div class="smallBox"><div class="arrowLeft">←</div><div class="smallPicBox"> //⽤⼀个div存放⼀个ul,并对ul⾥的每⼀个li进⾏初始样式设置<ul><li id="li1" style="position: absolute;top:calc(50% - 200px);left: 0px;z-index:1;background-color: aqua;transform: scale(0.5);transition: 0.3s;opacity: 0.5;background-image: url(./images/01.jpg);background-size: cover;"></li><li id="li2" style="position: absolute;top:calc(50% - 200px);left: 150px; z-index:2;background-color: red;transform: scale(0.7); transition: 0.3s;opacity: 0.7;background-image: url(./images/02.jpg);background-size: cover;"></li><li id="li3" style="position: absolute;top:calc(50% - 200px);left: 300px; z-index:3;background-color: blue;transform: scale(0.9); transition: 0.3s;opacity: 0.9;background-image: url(./images/03.jpg);background-size: cover;"></li><li id="li4" style="position: absolute;top:calc(50% - 200px);left: 450px; z-index:4;background-color: green;transform: scale(1); transition: 0.3s;opacity: 1;background-image: url(./images/04.jpg);background-size: cover;"></li><li id="li5" style="position: absolute;top:calc(50% - 200px);left: 600px; z-index:3;background-color: yellow;transform: scale(0.9); transition: 0.3s;opacity: 0.9;background-image: url(./images/05.jpg);background-size: cover;"></li> <li id="li6" style="position: absolute;top:calc(50% - 200px);left: 750px; z-index:2;background-color: gray;transform: scale(0.7); transition: 0.3s;opacity: 0.7;background-image: url(./images/06.jpg);background-size: cover;"></li><li id="li7" style="position: absolute;top:calc(50% - 200px);left: 900px; z-index:1;background-color: deeppink;transform: scale(0.5); transition: 0.3s;opacity: 0.5;background-image: url(./images/07.jpg);background-size: cover;"></li> </ul></div><div class="arrowRight">→</div></div></div></body>CSS代码:<style>*{margin: 0;padding: 0;list-style: none;}.albumBox{width: 1200px;height: 400px;margin: 0 auto;border: 1px solid #000;}.smallBox{height: 400px;line-height: 400px;position: relative;}.smallPicBox{width: 1100px;height: 400px;float: left;position: relative;}.smallPicBox ul{width: 100%;height: 400px;}.smallPicBox li{width: 320px;height: 400px;float: left;border: 1px solid #000;box-sizing: border-box;}.smallBox .current::after{content: "\25b2";position: absolute;top: -31px;left: 70px;color: red;}.arrowLeft{width: 50px;height: 400px;position: absolute;left: 0;border: 1px solid #000;box-sizing: border-box;background-color: gray;z-index: 10;}.arrowRight{width: 50px;height: 400px;position: absolute;right: 0;border: 1px solid #000;box-sizing: border-box;background-color: gray;z-index: 10;}</style>JS代码:<script>var arrowLeft=document.querySelector(".arrowLeft")var arrowRight=document.querySelector(".arrowRight")var ul=document.querySelector(".smallPicBox ul")var li=document.querySelectorAll(".smallPicBox li")var timerId=0arrowLeft.onclick=function(){ //左箭头点击事件li=document.getElementsByTagName('li')ul.appendChild(li[0]) //将ul的第0个⼦元素剪切到末尾,实现⽆缝轮播posi(li) //修改每⼀个li的属性}arrowRight.onclick=function(){ //右箭头点击事件li=document.getElementsByTagName('li') //重新获取liul.insertBefore(li[6],li[0]) //将ul的最后⼀个⼦元素剪切到最前⾯,实现⽆缝轮播posi(li) //修改每⼀个li的属性}function posi(li){ //修改li属性函数var n1=0for(var x=0;x<li.length;x++){ //修改位置li[x].style.left=n1+'px'n1=n1+150}for(var i=0;i<li.length/2;i++){ //修改层级li[i].style.zIndex=i+1li[li.length-1-i].style.zIndex=i+1}li[3].style.zIndex='4'var n2=0.3for(var j=0;j<li.length/2;j++){ //缩放n2=parseFloat(n2+0.2)li[j].style.transform='scale('+n2+')'li[li.length-1-j].style.transform='scale('+n2+')'}li[3].style.transform='scale(1)'var n3=0.3for(var k=0;k<li.length/2;k++){ //修改透明度n3=parseFloat(n3+0.2)li[k].style.opacity=n3li[li.length-1-k].style.opacity=n3}li[3].style.opacity='1'}//⿏标移⼊移出temerId=setInterval(function(){arrowLeft.click()}, 1000);arrowLeft.onmouseover=function(){clearInterval(temerId)}arrowLeft.onmouseout=function(){temerId=setInterval(function(){arrowLeft.click()}, 1000);}arrowRight.onmouseover=function(){clearInterval(temerId)}arrowRight.onmouseout=function(){temerId=setInterval(function(){arrowLeft.click()}, 1000);}</script>注:本例js是直接写在body⾥的,也可以单独写⼀个js⽂件,在引⼊到html界⾯效果图:到此这篇关于HTML+CSS+JS实现堆叠轮播效果的⽰例代码的⽂章就介绍到这了,更多相关HTML+CSS+JS实现轮播内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章,希望⼤家以后多多⽀持!。
JS+html--实现图片轮播
JS+html--实现图⽚轮播⼤家肯定见过某些⽹站⼀个炫酷的页⾯,就是图⽚轮播,也就是我们常说的幻灯⽚播放。
对于初学者来说,可能会有点头疼,没关系,⼩李在这给⼤家献上⾃⼰刚刚写好的关于图⽚轮播的代码。
以下功能的实现⽤了jQuery,⼤家可以去⽹上找⼀下关于jQuery的资源下载使⽤。
index.html1<!doctype html>2<html>3<head>4<meta charset="utf-8">5<title>幻灯⽚⽂档</title>6<link href="_css/slide.css" type="text/css" rel="stylesheet"/>7<script src="_js/jquery.min.js" type="text/javascript"></script>8<script src="_js/slide.js" type="text/javascript"></script>9</head>1011<body>12<h1>幻灯⽚设计</h1>13<!-- html页⾯通过li标签添加播放图⽚ -->14<div class="slider-container">15<ul id="slider" class="slider-wrapper">16<li class="slide-first">17<img src="_images/p1.jpg" alt="雪⼭天池"/>18<div class="caption">19<h3 class="caption-title"><a href="#">雪⼭天池</a></h3>20</div>21</li>22<li>23<img src="_images/p2.jpg" alt="未来之路"/>24<div class="caption">25<h3 class="caption-title"><a href="#">未来之路</a></h3>26</div>27</li>28<li>29<img src="_images/p3.jpg" alt="独⽴寒秋"/>30<div class="caption">31<h3 class="caption-title"><a href="#">独⽴寒秋</a></h3>32</div>33</li>34<li>35<img src="_images/p4.jpg" alt="⾼⼭流⽔"/>36<div class="caption">37<h3 class="caption-title"><a href="#">⾼⼭流⽔</a></h3>38</div>39</li>40<li>41<img src="_images/p5.jpg" alt="天堑变通途"/>42<div class="caption">43<h3 class="caption-title"><a href="#">天堑变通途</a></h3>44</div>45</li>46<li>47<img src="_images/p6.jpg" alt="远古的呼唤"/>48<div class="caption">49<h3 class="caption-title"><a href="#">远古的呼唤</a></h3>50</div>51</li>52<li>53<img src="_images/p7.jpg" alt="欲与天公试⽐⾼"/>54<div class="caption">55<h3 class="caption-title"><a href="#">欲与天公试⽐⾼</a></h3>56</div>57</li>58<li>59<img src="_images/p8.jpg" alt="⼈间仙境,室外桃园"/>60<div class="caption">61<h3 class="caption-title"><a href="#">雪⼭天池</a></h3>62</div>63</li>64<li>65<img src="_images/p9.jpg" alt="⼭不转⽔转"/>66<div class="caption">67<h3 class="caption-title"><a href="/">⼭不转⽔转</a></h3>68</div>69</li>70</ul>71<ul id="slider-controls" class="slider-controls"></ul>7273</div><!-- end of slider-container -->74<p>适⽤浏览器:Firefox、Chrome、Opera、Safari,不⽀持IE8以下浏览器</p> 75</body>76</html>slide.css1@charset "utf-8";234/* 整体设置 */5html {6 margin:0px;7 padding:0px;8 }9body {10 background:#FF9;11 font-size:62.5%;12 }1314/* 页⾯标题 */15h1 {16 font-family:"⾪书","宋体","Times New Roman", Times, serif;17 font-size:5em;18 text-align:center;19 color:red;20 margin:10px auto;21 }2223/* 图⽚容器的样式定义 */24.slider-container {25 margin:0px auto;26 background:#FFF;27 width:800px;28 }2930/* 图⽚列表的样式定义 */31ul {32 list-style-type:none;33 }34.slider-wrapper {35 margin:0px;36 padding:0px;37 position:relative;38 height:450px;39 width:100%;40 border:5px solid #69F;41 overflow:hidden;42 z-index:80;43 box-shadow:8px 8px 4px #999999;44 }45.slider-wrapper li {46 display:none;47 }48li.slide-first {49 display:block;50 }51.slider-wrapper li img {52 position:absolute;53 top:0px;54 left:0px;55 max-width:100%;56 height: auto;57 }5859/* 图⽚标题的样式定义 */60.caption {61 position:absolute;62 left:0px;63 bottom:0px;64 width:100%;65 padding 10px;66 background:rgba(0,0,0,0.6);67 transform:translateY(100%);68 -ms-transform:translateY(100%); /* IE 9 */69 -moz-transform:translateY(100%); /* Firefox */70 -webkit-transform:translateY(100%); /* Safari 和 Chrome */71 -o-transform:translateY(100%); /* Opera */72 }73.slider-wrapper li:hover .caption {74 transform:translateY(0%);75 -ms-transform:translateY(0%); /* IE 9 */76 -moz-transform:translateY(0%); /* Firefox */77 -webkit-transform:translateY(0%); /* Safari 和 Chrome */78 -o-transform:translateY(0%); /* Opera */79 transition:all 0.3s ease-in;80 -ms-transition:all 0.3s ease-in; /* IE 9 */81 -moz-transition:all 0.3s ease-in; /* Firefox */82 -webkit-transition:all 0.3s ease-in; /* Safari 和 Chrome */83 -o-transition:all 0.3s ease-in; /* Opera */84 }85.caption-title {86 font-size:1.6em;87 color:#6FF;88 font-weight:700;89 line-height:2em;90 }9192.caption-title a {93 color:#FFF;94 font-size:2em;95 text-decoration:none;9697 }98.caption-title a:hover {99 background:red;100101 }102.caption-title a:active {103 background:blue;104 }105106/* 提⽰信息 */107p {108 color:black;109 font-size:2em;110 text-align:center;111 margin:50px 0;112 line-height:2em;113 margin:20px auto;114 }115116/* 作者按钮 */117.author a {118 font-family:"宋体";119 color:white;120 text-decoration:none;121 font-size:2em;122 border-radius:10px;123 padding:5px 7px;124 background:linear-gradient(#33C,#6CC);125 }126.author a:hover {127 background:linear-gradient(#FCF,#000);128 }129/* 控制按钮 */130.slider-controls {131 text-align: center;132 margin-top: 15px;133 }134.slider-controls li {135 background:#FC6;136/*border-radius: 50%;*/137 display:inline-block;138 height: 12px;139 width: 12px;140 margin: 0px 4px;141 cursor: pointer;142 }143.slider-controls li.active {144 background: red;145 }146slide.js1/**2 * 幻灯⽚JS脚本3*/4 $(function() {5var SliderModule = (function() {6var pb = {};7 pb.el = $('#slider'); //el表达式8 pb.items = {9 panel: pb.el.find('li') // 获得li集合10 }1112// 变量13var SliderInterval,14 currentSlider = 0, //当前幻灯⽚15 nextSlider = 1, //下⼀张16 lengthSlider = pb.items.panel.length; // 幻灯⽚集合长度1718// 初始化19 pb.init = function(settings) {20this.settings = settings || {duration: 8000}21var output = ''; // 输出的html语⾔2223// 初始化24 SliderInit();2526for(var i = 0; i < lengthSlider; i++) {27if (i == 0) {28 output += '<li class="active"></li>';29 } else {30 output += '<li></li>';31 }32 }3334// 单击按钮时切换图⽚35 $('#slider-controls').html(output).on('click', 'li', function (e){36var $this = $(this);37if (currentSlider !== $this.index()) {38 changePanel($this.index());39 };40 });41 }4243// 初始化⽅法44var SliderInit = function() {45 SliderInterval = setInterval(pb.startSlider, pb.settings.duration);46 }4748 pb.startSlider = function() {49var panels = pb.items.panel,50 controls = $('#slider-controls li');5152if (nextSlider >= lengthSlider) {53 nextSlider = 0;54 currentSlider = lengthSlider-1;55 }5657// 淡出淡⼊效果58 controls.removeClass('active').eq(nextSlider).addClass('active');59 panels.eq(currentSlider).fadeOut('slow');60 panels.eq(nextSlider).fadeIn('slow');6162// 设置当前幻灯⽚63 currentSlider = nextSlider;64 nextSlider += 1;65 }6667// ⾃动切换幻灯⽚68var changePanel = function(id) {69 clearInterval(SliderInterval);70var panels = pb.items.panel,71 controls = $('#slider-controls li');7273// 幻灯⽚头尾74if (id >= lengthSlider) {75 id = 0;76 } else if (id < 0) {77 id = lengthSlider-1;78 }7980// 幻灯⽚淡出淡⼊81 controls.removeClass('active').eq(id).addClass('active');82 panels.eq(currentSlider).fadeOut('slow');83 panels.eq(id).fadeIn('slow');8485// 当前幻灯⽚和下⼀张86 currentSlider = id;87 nextSlider = id+1;8889//重新初始化90 SliderInit();91 }929394return pb;95 }());96// 图⽚切换速度 4000毫秒97 SliderModule.init({duration: 4000});98 });欢迎各位⼤神批评指正,相互提⾼!版权所有,允许转载,转载请注明出处,侵权必究!。
用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()清除掉。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Html、js图片轮播代码
现在基本上每个网站都有一个自动轮播的banner广告图,在没有任何按钮的情况下,图片在规定的时间内进行自动切换。
下面是html、js 图片轮播代码详情。
代码效果图实例:
第一秒:
第二秒:
第三秒:
详细代码如下:Html代码部分:
<body>
<ul class="banner">
<li><img src="images/1.jpg"></li>
<li><img src="images/2.jpg"></li>
<li><img src="images/3.jpg"></li>
</ul>
</body>
Css代码部分:
<style type="text/css">
ul,li{margin:0px;padding:0px}
li{list-style:none}
.banner{width:630px;height:250px;margin:100px auto;overflow:hidden;cursor:pointer} </style>
Javascript代码部分:
<script type="text/javascript"src="jquery.js"></script>
<script type="text/javascript">
$(function(){
var timer=setInterval(function(){
if($(".banner li:last").is(":hidden")){
$(".banner li:visible").addClass("on");
$(".banner li[class=on]").next().fadeIn("slow");
$(".banner li[class=on]").hide().removeClass("on");
}else{
$(".banner li:last").hide();
$(".banner li:first").fadeIn("slow");
}
},2000)
$(".banner li").hover(function(){
clearInterval(timer);
},function(){
timer=setInterval(function(){
if($(".banner li:last").is(":hidden")){
$(".banner li:visible").addClass("on");
$(".banner li[class=on]").next().fadeIn("slow");
$(".banner li[class=on]").hide().removeClass("on");
}else{
$(".banner li:last").hide();
$(".banner li:first").fadeIn("slow");
}
},2000)
})
})
</script>
【Jquery.js】是一个jquery库,只要百度一下搜索“jquery”下载这个文件就行。
以上效果中所用的图片可以根据自己的情况来进行替换,只要代码没问题,就肯定有效果的。
赵一鸣随笔博客中有更多的javascript效果写法的高清视频,搜索一下就能找到。
如果有兴趣,可以去大型网站看一下这种效果,特别是商城类网站,他们做的都很漂亮,例如聚美优品、淘宝、天猫都有,但是以上所写的代码是做这个效果最简单实用的,可以参考!。