自己用原生JS写的轮播图,支持移动端触摸滑动,分页器圆点可以支持mouseover鼠标移入和click点击,高手看了勿喷哈
自己用原生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点击,高手看了勿喷哈的更多相关文章
- 自己用原生JS写的轮播图,支持移动端触屏滑动,面向对象思路。分页器圆点支持click和mouseover。
自己用原生javascript写的轮播图,面向对象思路,支持移动端手指触屏滑动.分页器圆点可以选择click点击或mouseover鼠标移入时触发.图片滚动用的setInterval,感觉setInt ...
- 原生js写简单轮播图方式1-从左向右滑动
轮播图就是让图片每隔几秒自动滑动,达到图片轮流播放的效果.轮播图从效果来说有滑动式的也有渐入式的,滑动式的轮播图就是图片从左向右滑入的效果,渐入式的轮播图就是图片根据透明度渐渐显示的效果,这里说的是实 ...
- 原生JS实现简易轮播图
原生JS实现简易轮播图(渐变?) 最近做网页总是会用到轮播图,我就把之前写的轮播图单独拿出来吧,如果有...如果真的有人也需要也可以复制去用用啊..哈~.. window.onload = funct ...
- jQuery与原生js实现banner轮播图
jQuery与原生js实现banner轮播图: (jq需自己加载)(图片需自己加载) <!DOCTYPE html> <html> <head> <meta ...
- 原生 js 左右切换轮播图
使用方法: 可能很多人对轮播图感兴趣,下面奉上本人的 原生 js 轮播代码复制 js 到页面的最底部,样式在 css 里改,js 基本不用动,有什么不懂的可以 加本人 QQ172360937 咨询 或 ...
- 原生JS实现旋转木马轮播图特效
大概是这个样子: 首先来简单布局一下(emm...随便弄一下吧,反正主要是用js来整的) <!DOCTYPE html> <html lang="en"> ...
- photoSlider-html5原生js移动开发轮播图-相册滑动插件
简单的移动端图片滑动切换浏览插件 分别引用css文件和js文件 如: <link rel="stylesheet" type="text/css" hre ...
- 用 JS 写 (轮播图 / 选项卡 / 滑动门)
页面中经常会用到各式各样的轮播图,今天贺贺为大家介绍一种常用的方法,对于JS我们需要举一反三,一种方法可以对多个轮播样式进行渲染. <head> <meta charset=&quo ...
- 【原生JS】层叠轮播图
又是轮播?没错,换个样式玩轮播. HTML: <!DOCTYPE html> <html lang="en"> <head> <meta ...
随机推荐
- [EffectiveC++]item04:Make sure the objects are initialized before they're used
28 页 C++规定,对象的成员变量的初始化动作发生在进入构造函数本体之前. 构造函数的一个较佳的写法是,使用所谓的member initialization list替换赋值动作. 29页 但请立下 ...
- (转)如何解决VC中的警告warning C4251 needs to have dll-interface
这通常是由于以数据成员方式在DLL导出类中使用了模板类造成的.比如: #include <iostream> #include <vector> using namespace ...
- JS数据模板分离(告别字符串拼接)-template
刚开始在写第一个动态网页的demo时,由于html不多,便使用字符串拼接的方法添加到dom来渲染,可是在后来写某外卖app时也需要如此添加,打开代码一看几千行,突然感觉累觉不爱 一行行的拼接有这功夫别 ...
- Math Summary 数论总结
一.素数 Miller-Rabin 首先介绍一下伪素数:若n是一个正整数,且存在正整数a满足$a^{n-1}\equiv1\;(mod\;n)$ (费马小定理,但n不一定为素数) 那么我们说n是基于a ...
- 【node.js】事件循环、EventEmitter
Node.js 是单进程单线程应用程序,但是通过事件和回调支持并发,所以性能非常高. Node.js 的每一个 API 都是异步的,并作为一个独立线程运行,使用异步函数调用,并处理并发. 事件驱动程序 ...
- 2、Android-UI(关于Nine-Patch图片)
实例: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...
- PHP异步:在PHP中使用 fsockopen curl 实现类似异步处理的功能
PHP从主流来看,是一门面向过程的语言,它的最大缺点就是无法实现多线程管理,其程序的执行都是从头到尾,按照逻辑一路执行下来,不可能出现分支,这一点是限制php在主流程序语言中往更高级的语言发展的原因之 ...
- PHPStorm自定义主题配置
1.下载喜欢的主题 官方下载地址:下载 2.将.icls主题文件放到PHPStorm的配置中 windows下主题位置:C:\Users\Administrator\.PhpStorm2017.3\c ...
- background-position详解
一.background-position:left top; 背景图片的左上角和容器(container)的左上角对齐,超出的部分隐藏.等同于 background-position:0,0;也等同 ...
- Jmeter服务器监控插件使用
Jmeter服务器监控插件使用 Jmeter-Plugins支持CPU.Memory.Swap.Disk和Network的监控,在测试过程中更加方便进行结果收集和统计分析. 一.准备工作: 1.下载J ...