导航:首页 > 编程语言 > 手机js轮播

手机js轮播

发布时间:2021-12-04 02:52:37

1. 手机app滑动轮播原生js怎么写

下面是提供的一个demo:
1、html

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content = "width=device-width, target-densitydpi=high-dpi, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta content="telephone=no" name="format-detection" />
<meta name="keywords" content="seokeywords"/>
<meta name="description" content="seodescription"/>
<title>mggScrollImg demo 作者:js明哥哥</title>
<style>
ul,li{margin:0;padding:0;}
@media screen and (min-width:240px) {
html, body{
font-size:9px;
}
}
@media screen and (min-width:320px) {
html, body{
font-size:12px;
}
}
@media screen and (min-width:480px) {
html, body{
font-size:18px;
}
}
@media screen and (min-width:640px) {
html, body{
font-size:24px;
}
}
@media screen and (min-width:960px) {
html, body{
font-size:36px;
}
}
div.imgbox{width:25rem;height:16.5rem;overflow:hidden;margin:0 auto;}
div.imgbox ul{clear:both;width:75rem;}
div.imgbox ul li{float:left;width:25rem;height:16.5rem;overflow:hidden;text-align:center;}
div.imgbox ul li img{width:24rem;height:16.5rem;}
#page{color:red;}
</style>
</head>
<body>
<div class="imgbox">
<ul>
<li><a href="http://home.cnblogs.com/u/huming/"><img src="http://y2.ifengimg.com/df84c07b46e03f8e/2014/0512/rdn_53708f3d8533e.jpg"></img></a></li>
<li><a href="http://home.cnblogs.com/u/huming/"><img src="http://y2.ifengimg.com/df84c07b46e03f8e/2014/0512/rdn_53708f23aad06.jpg"></img></a></li>
<li><a href="http://home.cnblogs.com/u/huming/"><img src="http://y2.ifengimg.com/df84c07b46e03f8e/2014/0512/rdn_53708f345282b.jpg"></img></a></li>
</ul>
</div>
<div>这里通过回调显示当前滚动到多少页:<span id="page">0</span></div>
<script src="http://zeptojs.com/zepto.min.js"></script>
<script src="mggScrollImg.js"></script>
<script>
(function(){
/*
注意:$.mggScrollImg返回的scrollImg对象上有
next,prev,go三个方法,可以实现外部对滚动索引的控制。
如:scrollImg.next();//会切换到下一张图片
scrollImg.go(0);//会切换到第一张图片
*/
var scrollImg = $.mggScrollImg('.imgbox ul',{
loop : true,//循环切换
auto : true,//自动切换
callback : function(ind){//这里传过来的是索引值
$('#page').text(ind+1);
}
});
})()
</script>
</body>
</html>


2、核心滑动脚本代码

(function($){
/*
图片滚动效果
add 2014-05-14 by js明哥哥
博客地址:http://home.cnblogs.com/u/huming/
@jQuery or @String box : 滚动列表jQuery对象或者选择器 如:滚动元素为li的外层ul
@object config : {
@Number width : 一次滚动宽度,默认为box里面第一个一级子元素宽度[如果子元素宽度不均匀则滚动效果会错乱]
@Number size : 列表长度,默认为box里面所有一级子元素个数[如果size不等于一级子元素个数,则不支持循环滚动]
@Boolean loop : 是否支持循环滚动 默认 true
@Boolean auto : 是否自动滚动,支持自动滚动时必须支持循环滚动,否则设置无效,默认为true
@Number auto_wait_time : 自动轮播一次时间间隔,默认为:3000ms
@Function callback : 滚动完回调函数,参入一个参数当前滚动节点索引值
}
*/
function mggScrollImg(box,config){
this.box = $(box);
this.config = $.extend({},config||{});
this.width = this.config.width||this.box.children().eq(0).width();//一次滚动的宽度
this.size = this.config.size||this.box.children().length;
this.loop = this.config.loop||true;//默认能循环滚动
this.auto = this.config.auto||true;//默认自动滚动
this.auto_wait_time = this.config.auto_wait_time||3000;//轮播间隔
this.scroll_time = 300;//滚动时长
this.minleft = -this.width*(this.size-1);//最小left值,注意是负数[不循环情况下的值]
this.maxleft =0;//最大lfet值[不循环情况下的值]
this.now_left = 0;//初始位置信息[不循环情况下的值]
this.point_x = null;//记录一个x坐标
this.point_y = null;//记录一个y坐标
this.move_left = false;//记录向哪边滑动
this.index = 0;
this.busy = false;
this.timer;
this.init();
}
$.extend(mggScrollImg.prototype,{
init : function(){
this.bind_event();
this.init_loop();
this.auto_scroll();
},
bind_event : function(){
var self = this;
self.box.bind('touchstart',function(e){
if(e.touches.length==1 && !self.busy){
self.point_x = e.touches[0].screenX;
self.point_y = e.touches[0].screenY;
}
}).bind('touchmove',function(e){
if(e.touches.length==1 && !self.busy){
return self.move(e.touches[0].screenX,e.touches[0].screenY);//这里根据返回值觉得是否阻止默认touch事件
}
}).bind('touchend',function(e){
!self.busy && self.move_end();
});
},
/*
初始化循环滚动,当一次性需要滚动多个子元素时,暂不支持循环滚动效果,
如果想实现一次性滚动多个子元素效果,可以通过页面结构实现
循环滚动思路:复制首尾节点到尾首
*/
init_loop : function(){
if(this.box.children().length == this.size && this.loop){//暂时只支持size和子节点数相等情况的循环
this.now_left = -this.width;//设置初始位置信息
this.minleft = -this.width*this.size;//最小left值
this.maxleft = -this.width;
this.box.prepend(this.box.children().eq(this.size-1).clone()).append(this.box.children().eq(1).clone()).css(this.get_style(2));
this.box.css('width',this.width*(this.size+2));
}else{
this.loop = false;
this.box.css('width',this.width*this.size);
}
},
auto_scroll : function(){//自动滚动
var self = this;
if(!self.loop || !self.auto)return;
clearTimeout(self.timer);
self.timer = setTimeout(function(){
self.go_index(self.index+1);
},self.auto_wait_time);
},
go_index : function(ind){//滚动到指定索引页面
var self = this;
if(self.busy)return;
clearTimeout(self.timer);
self.busy = true;
if(self.loop){//如果循环
ind = ind<0?-1:ind;
ind = ind>self.size?self.size:ind;
}else{
ind = ind<0?0:ind;
ind = ind>=self.size?(self.size-1):ind;
}
if(!self.loop && (self.now_left == -(self.width*ind))){
self.complete(ind);
}else if(self.loop && (self.now_left == -self.width*(ind+1))){
self.complete(ind);
}else{
if(ind == -1 || ind == self.size){//循环滚动边界
self.index = ind==-1?(self.size-1):0;
self.now_left = ind==-1?0:-self.width*(self.size+1);
}else{
self.index = ind;
self.now_left = -(self.width*(self.index+(self.loop?1:0)));
}
self.box.css(this.get_style(1));
setTimeout(function(){
self.complete(ind);
},self.scroll_time);
}
},
complete : function(ind){//动画完成回调
var self = this;
self.busy = false;
self.config.callback && self.config.callback(self.index);
if(ind==-1){
self.now_left = self.minleft;
}else if(ind==self.size){
self.now_left = self.maxleft;
}
self.box.css(this.get_style(2));
self.auto_scroll();
},
next : function(){//下一页滚动
if(!this.busy){
this.go_index(this.index+1);
}
},
prev : function(){//上一页滚动
if(!this.busy){
this.go_index(this.index-1);
}
},
move : function(point_x,point_y){//滑动屏幕处理函数
var changeX = point_x - (this.point_x===null?point_x:this.point_x),
changeY = point_y - (this.point_y===null?point_y:this.point_y),
marginleft = this.now_left, return_value = false,
sin =changeY/Math.sqrt(changeX*changeX+changeY*changeY);
this.now_left = marginleft+changeX;
this.move_left = changeX<0;
if(sin>Math.sin(Math.PI/3) || sin<-Math.sin(Math.PI/3)){//滑动屏幕角度范围:PI/3 -- 2PI/3
return_value = true;//不阻止默认行为
}
this.point_x = point_x;
this.point_y = point_y;
this.box.css(this.get_style(2));
return return_value;
},
move_end : function(){
var changeX = this.now_left%this.width,ind;
if(this.now_left<this.minleft){//手指向左滑动
ind = this.index +1;
}else if(this.now_left>this.maxleft){//手指向右滑动
ind = this.index-1;
}else if(changeX!=0){
if(this.move_left){//手指向左滑动
ind = this.index+1;
}else{//手指向右滑动
ind = this.index-1;
}
}else{
ind = this.index;
}
this.point_x = this.point_y = null;
this.go_index(ind);
},
/*
获取动画样式,要兼容更多浏览器,可以扩展该方法
@int fig : 1 动画 2 没动画
*/
get_style : function(fig){
var x = this.now_left ,
time = fig==1?this.scroll_time:0;
return {
'-webkit-transition':'-webkit-transform '+time+'ms',
'-webkit-transform':'translate3d('+x+'px,0,0)',
'-webkit-backface-visibility': 'hidden',
'transition':'transform '+time+'ms',
'transform':'translate3d('+x+'px,0,0)'
};
}
});
/*
这里对外提供调用接口,对外提供接口方法
next :下一页
prev :上一页
go :滚动到指定页
*/
$.mggScrollImg = function(box,config){
var scrollImg = new mggScrollImg(box,config);
return {//对外提供接口
next : function(){scrollImg.next();},
prev : function(){scrollImg.prev();},
go : function(ind){scrollImg.go_index(parseInt(ind)||0);}
}
}
})(Zepto)

2. js如何实现新闻轮播

编写基本的样
<div class="slide" id="slide">
<div class="img-div">
<img src="img/banner1.png" title="图片1"/>
<img src="img/banner2.png" title="图片2"/>
<img src="img/banner3.png" title="图片3"/>
</div>
<div class="slide-btn">
<a href="#" class="hover">●</a>
<a href="#">●</a>
<a href="#">●</a>
</div>
</div>

获取文档对象
以id获取文档对象
$id:function(id){return document.getElementById(id);},
//以标签名获取文档对象
$tag:function(tagName,obj){return (obj ?obj : document).getElementsByTagName(tagName); },
以class获取文档对象
$c:function (claN,obj){
var tag = $tag('*'),reg = new RegExp('(^|\\s)'+claN+'(\\s|$)'),arr=[];
for(var i=0;i<tag.length;i++){
if (reg.test(tag[i].className)){
arr.push(tag[i]);
}
}
return arr;
}

添加和移除class
$add:function(obj,claN){
reg = new RegExp('(^|\\s)'+claN+'(\\s|$)');
if (!reg.test(obj.className)){

obj.className += ' '+claN;
}
},
//移除classs
$remve:function(obj,claN){var cla=obj.className,reg="/\\s*"+claN+"\\b/g";obj.className=cla?cla.replace(eval(reg),''):''},

css获取方法
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方法
easing ={
linear:function(t,b,c,d){return c*t/d + b;},
swing:function(t,b,c,d) {return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;},
easeIn:function(t,b,c,d){return c*(t/=d)*t*t*t + b;},
easeOut:function(t,b,c,d){return -c*((t=t/d-1)*t*t*t - 1) + b;},
easeInOut:function(t,b,c,d){return ((t/=d/2) < 1)?(c/2*t*t*t*t + b):(-c/2*((t-=2)*t*t*t - 2) + b);}
}

初始化
config:{
index:0,
auto:true,
direct:'left' //滚动方向,left,top,
},
init:function(){
this.slide = this.$id('slide');
this.img_div = this.$c('img-div')[0],
this.slide_btn = this.$tag('a',this.$c('slide-btn')[0]);
this.img_arr = this.$tag('img',this.img_div);
if(this.config.auto) this.play();//是否自动播放
this.hover();//绑定导航悬停切换。即mouseover,mouseout事件
},

动画效果
animate:function(obj,attr,val){
var d = 1000;//动画时间一秒完成。
if(obj[attr+'timer']) clearInterval(obj[attr+'timer']);
var start = parseInt(zBase.css(obj,attr));//动画开始位置
//space = 动画结束位置-动画开始位置,即动画要运动的距离。
var space = val- start,st=(new Date).getTime(),m=space>0? 'ceil':'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;

for(var j=0;j<zBase.slide_btn.length;j++){
zBase.$remve(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*500);//500是轮播器的大小,当前索引乘以500,再取负,就是轮播器要滚动f到的位置。
}
this.slide_btn[i].onmouseout = function(){
zBase.play();
}
}
},

自动播放
play:function(){
this.slide.timer = setInterval(function(){
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.$remve(zBase.slide_btn[j],'hover') ;
}
zBase.$add(zBase.slide_btn[zBase.config.index],'hover');

},3000)
},

调用代码
zBase.init();

3. Js做好手机端遮罩,导航拖拽,正常,加上自动轮播Js后,导航的误点跟随变色出问题,遮罩出问题!求大

你在轮播图片的时候,不让用户点击导航就好了,等轮播停止后才能点击

4. js,怎么给banner大图轮播加触摸事件

什么触摸事件???是在手机可以用手滑他就可以切换吗?

5. 用jquery或js实现三个div自动循环轮播

可以参考下面的daima :

//3个div的统一class = 'div'

var index =0;

//3秒轮播一次

var timer = setInterval(function(){

index = (index == 2) ? 0 : index + 1;

//某个div显示,其他的隐藏

$(".div").hide().eq(index).show();

}, 3000);

(5)手机js轮播扩展阅读专:

javaScript参考函数

getUTCFullYear() 根据邦属际时间来失掉完全的年份

getUTCMonth() 依据国际时间来得到月份(0-11)

getUTCDate() 依据国际时间来失掉日(1-31)

getUTCHours() 依据国际时间来失掉小时(0-23)

getUTCMinutes() 根据邦际光阴来往归分钟(0-59)

getUTCMilliseconds()依据国际时间来返回毫秒(0-999)

6. oppo官网下的圆环轮播用js怎么写

你好,如果下载的是otxt格式的无法打开,只能在oppo电子书上打开,如果是普通的txt可能是编码错误,可以发帖向版主报告一下,带来不便深感抱歉。

7. 如何用css3实现手机端轮播+js

建议用插件 轮播可以参考插件swiper

8. 简单的HTML+js图片轮播

h5代码:

<div id=“wrap”><ul id=“list”><li>10</li><li>9</li><li>8</li><li>7</li><li>6</li><li>5</li><li>4</li><li>3</li><li>2</li><li>1</ul></div>

css代码:

<style type="text/css">@-webkit-keyframes move{0%{left:-500px;}100%{left:0;}}#wrap{width:600px;height:130px;border:1px solid #000;position:relative;margin:100px auto;overflow:

hidden;}#list{position:absolute;left:0;top:0;padding:0;margin:0;-webkit-animation:5s move infinite linear;width:200%;}#list li{list-style:none;width:120px;height:130px;border:1px solid red;background: pink;color:#fff;text-align: center;float:left;font:normal 50px/2.5em '微软雅黑';}#wrap:hover #list{-webkit-animation-play-state:paused;}</style>

(8)手机js轮播扩展阅读:

轮播图是网站介绍其主要产品或重要信息的一种方式。简单的一点是,在网页的某一部分,会依次呈现几个带有重要信息的图片,这样访问者就可以快速了解网站想要表达的主要信息。各种新闻网站的头版头条也是以这种方式呈现的重要信息。

轮播图的实现方式:例如:有5张轮播的图片,每张图片的宽度为1024px,高度为512px。那么旋转的窗口大小应该是一张图片的大小,即1024×512,然后,将五张0px的图片水平连接,形成一张5120px宽、512px高的图片,最后,通过每次向左移动1024px,可以旋转大的合成图像。

9. 用js做的图片轮播 无法在手机上正常显示怎么办

不能正常显示应该不是css的问题啦,应该是js的问题了。试试这个插件吧http://www.superslide2.com/ 足够满足你的需求了

10. 怎么用js做一个简单的轮播图

obj1.onmouseover = function () {
clearInterval(time);
}
obj1.onmouseout = function () {
time = setInterval("turn();", 6000);
}
for (var num = 0; num < obj2.length; num++) {
obj2[num].onmouseover = function () {
turn(this.innerHTML);
clearInterval(time);
}
obj2[num].onmouseout = function () {
time = setInterval("turn();", 6000);
}
}

阅读全文

与手机js轮播相关的资料

热点内容
在线处理psd文件 浏览:367
古代电影免费在线观看 浏览:711
淫小说网址 浏览:898
如何在电脑上用数字编程 浏览:178
神豪系统类小说 浏览:46
oa系统怎样批量下载文件 浏览:299
mpp大数据体系 浏览:144
穿越雪中悍刀行收女主的小说 浏览:54
奇怪的美发沙龙剧情介绍 浏览:969
《沐风之女》电影巴巴鱼 浏览:4
psd文件如何转换成cdr 浏览:907
英语课作弊2电影 浏览:584
光盘里文件格式 浏览:423
易天下 著; 浏览:530
struts2上传文件json 浏览:640
可以看那种片的网站 浏览:671
重生古惑仔千人晒马的小说 浏览:15
火影小说 浏览:897
小说女主叫苏沐男主叫陆修 浏览:148
如懿传小说完整版txt 浏览:267

友情链接