自己用原生JavaScript写的轮播图,分页器圆点按钮可支持click点击,也可支持mouseover鼠标悬浮触发,同时支持移动端触摸滑动,有兴趣的友友可以试试哈,菜鸟一枚,高手看了勿喷,请多多指正哈:

HTML文档做了移动端优化,按照750px的设计稿哈。HTML这个元素的font-size是document.documentElement.clientWidth / 7.5,所以rem是动态的。

感觉不足之处是动画平滑度不太好。应该再改改偏移量和时间间隔,还有一个因素是用setInterval做的滚动图,setinterval不适合做动画、滚动图,不平滑。应该用setTimeout递归效果更好。

轮播图函数carouselFunc 接受的参数还没有改成对象,等以后再改吧。

JavaScript和CSS都写在HTML文档里了,没分开。直接上源码哈:

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
<script type="text/javascript">
var viWidth=document.documentElement.clientWidth;
if(viWidth<750){
document.documentElement.style.fontSize=viWidth/7.5+"px";
}else{
document.documentElement.style.fontSize="100px";
}
</script>
<title>轮播图移动端试验</title>
<style type="text/css">
html{
height:100%;
text-align: center;
}
body{
width:7.5rem;
height:100%;
margin:0 auto;
text-align: center;
}
.clearfix:after{
content:"";
display: block;
clear: both;
height:0px;
}
.clearfix{
zoom:1;
}
.myTitle{
margin:0px;
line-height:0.6rem;
height:0.6rem;
padding-top:0.2rem;
padding-bottom:0.2rem;
text-align:center;
}
.carousel1{
width:7rem;
height:3.5rem;
margin-left:auto;
margin-right:auto;
position: relative;
overflow:hidden;
}
.viewportA{
width:7rem;
height:3.5rem;
position: relative;
overflow: hidden;
z-index: 1;
}
.parentA{
width:1000%;
position: absolute;
}
.parentA .item{
float:left;
position: relative;
width:7rem;
height:3.5rem;
margin:0px;
display:inline;
cursor: pointer;
}
.parentA .item img{
display: block;
width:7rem;
height:3.5rem;
}
.carousel1 .prevA{
position:absolute;
height:10%;
width:auto;
left:0;
top:45%;
z-index:3;
opacity:0.4;
cursor: pointer;
}
.carousel1 .nextA{
position:absolute;
height:10%;
width:auto;
right:0;
top:45%;
z-index:3;
opacity:0.4;
cursor: pointer;
}
.carousel1 .prevA:hover {
opacity:0.9;
}
.carousel1 .nextA:hover {
opacity:0.9;
}
.circleListA{
position:absolute;
z-index: 3;
bottom:0.2rem;
width:1.05rem;
height:0.15rem;
left:50%;
margin-left:-0.525rem;
padding-top:0.03rem;
padding-bottom:0.03rem;
zoom:1;
background-color:rgba(255,255,255,0.5);
border-radius:0.1rem;
}
.circleListA span{
float:left;
height:0.15rem;
width:0.15rem;
border-radius:70%;
margin-right:0.05rem;
background-color:white;
cursor: pointer;
}
.circleListA span:hover{
background:#ffd700;
}
.circleListA .active{
background:#ff4500!important;
}
</style>
</head>
<body>
<h1 class="myTitle"></h1>
<div class="carousel1" id="carousel1">
<div class="viewportA" id="viewportA">
<div id="parentA" class="parentA clearfix" style="left:-7rem">
<div class="item"><img src="data:image/5.jpg" width="100%"></div>
<div class="item"><img src="data:image/1.jpg" width="100%"></div>
<div class="item"><img src="data:image/2.jpg" width="100%"></div>
<div class="item"><img src="data:image/3.jpg" width="100%"></div>
<div class="item"><img src="data:image/4.jpg" width="100%"></div>
<div class="item"><img src="data:image/5.jpg" width="100%"></div>
<div class="item"><img src="data:image/1.jpg" width="100%"></div>
</div>
</div>
<img src="data:image/btn-left.png" id="prevA" class="prevA" alt="点击->上一张">
<img src="data:image/btn-right.png" id="nextA" class="nextA" alt="点击->下一张">
<div id="circleListA" class="circleListA clearfix">
<span data-index="1" class="active" style="margin-left:0.05rem"></span>
<span data-index="2" class=""></span>
<span data-index="3" class=""></span>
<span data-index="4" class=""></span>
<span data-index="5" class=""></span>
</div>
</div>
<script type="text/javascript">
//轮播图构造函数 function carouselFunc(viewport,parent,listPar,btnName,prev,next,bow,globalT,step,c_Or_Mo){
var list=listPar.getElementsByTagName(btnName);
var len=list.length,lim=len-1,i=0;
var oriTarget=list[0];
var jg=globalT/step;
var timer,autoTimer,timerTo,outWaitT;
var t=4000;
var luoji=true,flag=false;
var startX,startY,endX,endY,nowX,nowY;
var offX,offY;
if(bow>0){
bow=-bow;
}
function onceScrollFunc(target){
var j=Number(target.getAttribute("data-index"));
var k=Number(oriTarget.getAttribute("data-index"));
if(target.className!="active"){
oriTarget.className="";
target.className="active";
oriTarget=target;
i=j-1;
var oriLoc=parent.offsetLeft;
var targetLoc=bow*j;
clearInterval(timer);
clearInterval(autoTimer);
if(targetLoc<oriLoc){
var py=(targetLoc-oriLoc)/step;
timer=setInterval(function(){
if(oriLoc>targetLoc){
oriLoc+=py;
parent.style.left=oriLoc+"px";
} else{
clearInterval(timer);
parent.style.left=targetLoc+"px"; }
},jg);
}else if(targetLoc>oriLoc){
var py=(targetLoc-oriLoc)/step;
timer=setInterval(function(){
if(oriLoc<targetLoc){
oriLoc+=py;
parent.style.left=oriLoc+"px";
} else{
clearInterval(timer);
parent.style.left=targetLoc+"px"; }
},jg);
}
}
}
if(c_Or_Mo=="mouseover"){
listPar.onmouseover=function(event){
timerTo=setTimeout(function(){
var e=event||window.event;
var target=e.target||e.srcElement;
if(target.tagName.toLowerCase()==btnName){
onceScrollFunc(target);
}
},120);
};
listPar.onmouseout=function(event){
var e=event||window.event;
var target=e.target||e.srcElement;
if(target.tagName.toLowerCase()==btnName){
clearTimeout(timerTo);
}
};
} else if(c_Or_Mo=="click"){
listPar.onclick=function(event){
var e=event||window.event;
var target=e.target||e.srcElement;
if(target.tagName.toLowerCase()==btnName){
onceScrollFunc(target);
}
}
}
prev.onclick=function(){
if(luoji){
luoji=false;
ScrollWidthFunc(0);
}
}
next.onclick=function(){
if(luoji){
luoji=false;
ScrollWidthFunc(1);
}
}
function ScrollWidthFunc(dir){
if(dir==1){
list[i].className="";
if(i<lim){
i+=1;
}else{
i=0;
parent.style.left="0px";
}
list[i].className="active";
oriTarget=list[i];
var nowScroll=parent.offsetLeft;
var objScroll=nowScroll+bow;
var py=bow/step;
timer=setInterval(function(){
if(nowScroll>objScroll){
nowScroll+=py;
parent.style.left=nowScroll+"px";
} else{
clearInterval(timer);
parent.style.left=objScroll+"px";
luoji=true;
}
},jg); } else{
list[i].className="";
if(i>0){
i-=1;
}else{
i=lim;
parent.style.left=bow*(len+1)+"px";
}
list[i].className="active";
oriTarget=list[i];
var nowScroll=parent.offsetLeft;
var objScroll=nowScroll-bow;
var py=bow/step;
timer=setInterval(function(){
if(nowScroll<objScroll){
nowScroll-=py;
parent.style.left=nowScroll+"px";
} else{
clearInterval(timer);
parent.style.left=objScroll+"px";
luoji=true;
}
},jg);
} }
if(document.documentElement.clientWidth<751){
parent.addEventListener('touchstart',function(event){
if(!flag){
clearInterval(autoTimer);
clearInterval(timer);
clearTimeout(outWaitT);
flag=true;
var e=event||window.event;
tStart(e);
}
},false);
parent.addEventListener('touchmove',function(event){
var e=event||window.event;
e.preventDefault();
tMove(e);
},false);
parent.addEventListener('touchend',function(event){
var e=event||window.event;
flag=false;
tEnd(e);
},false);
} else{
if(parent.addEventListener){
parent.addEventListener('mousedown',function(event){
if(!flag){
clearInterval(autoTimer);
clearInterval(timer);
clearTimeout(outWaitT);
flag=true;
var e=event||window.event;
tStart(e);
}
},false);
parent.addEventListener('mousemove',function(event){
var e=event||window.event;
e.preventDefault();
tMove(e);
},false);
parent.addEventListener('mouseup',function(event){
var e=event||window.event;
flag=false;
tEnd(e);
},false);
}else{
parent.attachEvent('onmousedown',function(event){
if(!flag){
clearInterval(autoTimer);
clearInterval(timer);
clearTimeout(outWaitT);
flag=true;
var e=event||window.event;
tStart(e);
}
});
parent.attachEvent('onmousemove',function(event){
var e=event||window.event;
e.preventDefault();
tMove(e);
});
parent.attachEvent('onmouseup',function(event){
var e=event||window.event;
flag=false;
tEnd(e);
});
}
} function tStart(e){
var touch;
if(e.type=='touchstart'){
touch=e.targetTouches[0];
}else{
touch=e;
}
startX=touch.pageX;
startY=touch.pageY;
offX=parent.offsetLeft;
offY=parent.offsetTop;
}
function tMove(e){
if(flag){
var touch;
if(e.type=='touchmove'){
touch=e.targetTouches[0];
}else{
touch=e;
}
nowX=touch.pageX-startX;
nowY=touch.pageY-startY;
parent.style.left=(offX+nowX)+"px";
}
}
function tEnd(e){
var touch;
if(e.type=='touchend'){
touch=e.changedTouches[0];
}else{
touch=e;
}
endX=touch.pageX;
var distance=parent.offsetLeft;
var s=Math.abs(endX-startX);
var judge=Math.abs(bow*0.2);
var lu=Math.abs(bow)-s;
var py=Math.abs(bow/step);
if(luoji){
if(s>judge){
if(distance<offX){
list[i].className="";
if(i<lim){
i+=1;
var objDis=bow*(Math.floor(Math.abs(distance)/Math.abs(bow))+1);
}else{
parent.style.left=-s+"px";
distance=-s;
var objDis=bow;
i=0;
}
list[i].className="active";
oriTarget=list[i];
timer=setInterval(function(){
if(distance>objDis){
distance-=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=objDis+"px";
if(e.type=='touchend'){
play(4000,1);
}
}
},jg);
}else{
list[i].className="";
if(i>0){
i-=1;
var objDis=bow*(Math.floor(Math.abs(distance)/Math.abs(bow)));
}else{
parent.style.left=(bow*(len+1)+s)+"px";
distance=bow*(len+1)+s;
var objDis=bow*len;
i=lim;
}
list[i].className="active";
oriTarget=list[i];
timer=setInterval(function(){
if(distance<objDis){
distance+=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=objDis+"px";
if(e.type=='touchend'){
play(4000,0);
}
}
},jg);
}
}else{
var targetP=bow*(Math.floor(Math.abs(offX)/Math.abs(bow)));
if(distance<targetP){
timer=setInterval(function(){
if(distance<targetP){
distance+=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=targetP+"px";
if(e.type=='touchend'){
play(4000,1);
}
}
},jg);
}else{
timer=setInterval(function(){
if(distance>targetP){
distance-=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=targetP+"px";
if(e.type=='touchend'){
play(4000,1);
}
}
},jg);
}
}
} else{
var targetP2=(i+1)*bow;
if(targetP2<distance){
timer=setInterval(function(){
if(distance>targetP2){
distance-=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=targetP2+"px";
if(e.type=='touchend'){
play(4000,1);
}
luoji=true;
}
},jg)
}else{
timer=setInterval(function(){
if(targetP2>distance){
distance+=py;
parent.style.left=distance+"px";
}else{
clearInterval(timer);
parent.style.left=targetP2+"px";
if(e.type=='touchend'){
play(4000,0);
}
luoji=true;
}
},jg)
}
}
} viewport.onmouseover=function(){
clearInterval(autoTimer);
clearTimeout(outWaitT);
}
viewport.onmouseout=function(event){
outWaitT=setTimeout(function(){
play(4000,1);
},1000);
}; function play(t,i){
autoTimer=setInterval(function(){
if(luoji){
luoji=false;
ScrollWidthFunc(i);
}
},t);
}
play(4000,1); }
function carousel1Func(){
var carousel1=document.getElementById("carousel1");
var viewportA=document.getElementById("viewportA");
var parentA=document.getElementById("parentA");
var listPar=document.getElementById("circleListA");
var btnName="span";
var prevA=document.getElementById("prevA");
var nextA=document.getElementById("nextA");
var globalT=500;
var step=50;
var bowA=-viewportA.clientWidth;
carouselFunc(carousel1,parentA,listPar,btnName,prevA,nextA,bowA,globalT,step,"mouseover");
}
if(window.addEventListener){
window.addEventListener("load",function(){
carousel1Func();
},false);
}else if(window.attachEvent){
window.attachEvent("onload",function(){
carousel1Func();
});
}
</script>
</body>
</html>

自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈的更多相关文章

  1. 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。

    自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...

  2. 原生js写简单轮播图方式1-从左向右滑动

    轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实 ...

  3. 原生JS实现简易轮播图

    原生JS实现简易轮播图(渐变?) 最近做网页总是会用到轮播图,我就把之前写的轮播图单独拿出来吧,如果有...如果真的有人也需要也可以复制去用用啊..哈~.. window.onload = funct ...

  4. jQuery与原生js实现banner轮播图

    jQuery与原生js实现banner轮播图: (jq需自己加载)(图片需自己加载) <!DOCTYPE html> <html> <head> <meta ...

  5. 原生 js 左右切换轮播图

    使用方法: 可能很多人对轮播图感兴趣,下面奉上本人的 原生 js 轮播代码复制 js 到页面的最底部,样式在 css 里改,js 基本不用动,有什么不懂的可以 加本人 QQ172360937 咨询 或 ...

  6. 原生JS实现旋转木马轮播图特效

    大概是这个样子: 首先来简单布局一下(emm...随便弄一下吧,反正主要是用js来整的) <!DOCTYPE html> <html lang="en"> ...

  7. photoSlider-html5原生js移动开发轮播图-相册滑动插件

    简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ...

  8. 用 JS 写 (轮播图 / 选项卡 / 滑动门)

    页面中经常会用到各式各样的轮播图,今天贺贺为大家介绍一种常用的方法,对于JS我们需要举一反三,一种方法可以对多个轮播样式进行渲染. <head> <meta charset=&quo ...

  9. 【原生JS】层叠轮播图

    又是轮播?没错,换个样式玩轮播. HTML: <!DOCTYPE html> <html lang="en"> <head> <meta ...

随机推荐

  1. Android Volley源码分析及扩展

    转载请标明出处: http://www.cnblogs.com/why168888/p/6681232.html 本文出自:[Edwin博客园] Volley 介绍 Android系统中主要提供了两种 ...

  2. jq判断鼠标滚轴向上滚动还是向下滚动

    $(document).on("mousewheel DOMMouseScroll", function (e) { var delta = (e.originalEvent.wh ...

  3. 【RabbitMQ】2、RabbitMQ入门程序——Hello World

      首先说一下,MQ全称为Message Queue消息队列是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们.消息传递指的是程序 ...

  4. 给trac的ticket添加提交时字段验证

    我们在项目管理中使用了trac系统,并且对于ticket添加了以下自定义字段并且对它们的格式都有一定要求: svn版本号:格式为 r1234.多个版本号之间使用半角逗号隔开.如:r1234,r5678 ...

  5. 【vue】饿了么项目-页面骨架开发

    1.页面骨架开发 1.1组件拆分 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(viewport)比屏幕宽,这样就不用把每个网页挤到很小的窗口中(这样会破坏没 ...

  6. numpy初始化

    一般的初始化就是用zeros这种去初始化,但你想测试一些函数的时候,如果是全0其实不好测试 可以先用python本身初始化一个list,然后转换成numpy的array a = [1.1,1.5,1. ...

  7. python学习day25 正则表达式

    4.30-25 正则表达式与re模块 re模块本身是用来操作正则表达式,与正则本身没有关系 正则表达式是指一规则,匹配字符串的规则 1.正则表达式regex 正则表达式regex是指一规则,匹配字符串 ...

  8. PDF压缩,在线压缩免费

    https://smallpdf.com/ 一个很牛逼的网站 https://zh.wikihow.com/ https://zh.wikihow.com/%E9%A6%96%E9%A1%B5

  9. +QFTPOPEN: 603,0 怎么把这样一个字符串中的 603 提取出来给一个 uint32_t 的变量那

    +QFTPOPEN: 603,0   怎么把这样一个字符串中的 603 提取出来给一个 uint32_t 的变量那? 注意冒号后面有个空格!!! 答案:要使用 sscanf 标准库函数! char s ...

  10. Blocking Master Example QT 自带 的 serial 即 串口 例子

    1.官方解释文档:http://doc.qt.io/qt-5/qtserialport-blockingmaster-example.html Blocking Master shows how to ...