移动端无缝滚动兼拖动插件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <title>图片点击滑动</title>
        <style>
            body{padding: 0;margin: 0;}
            li,ol,ul{margin: 0;list-style: none;padding: 0;}
            div#l-btn{left: 0;color: gray;}
            div#r-btn{right: 0;color:brown;cursor: pointer;}
            .nav-container{position: relative;width: 98%;height:10em;overflow: hidden;margin: 2em auto;}
            .nav{position: absolute;top: 0;left:0;}
            .nav li{float: left;width:8em;text-align: center;}
            .nav li img{width: 100%;height: auto;}
            .nav span{font-size: 1.6em;}
        </style>
    </head>
    <body>
        <div class="nav-container">
            <ul class="nav" id="nav">
                 <li><img src="img/hb1.jpg" /><br/><span>1</span></li>
                 <li><img src="img/hb2.jpg" /><br/><span>2</span></li>
                 <li><img src="img/hb3.jpg" /><br/><span>3</span></li>
                 <li><img src="img/hb4.jpg" /><br/><span>4</span></li>
                 <li><img src="img/hb2.jpg" /><br/><span>5</span></li>
                 <li><img src="img/hb3.jpg" /><br/><span>6</span></li>
                 <li><img src="img/hb4.jpg" /><br/><span>7</span></li>
                 <li><img src="img/hb2.jpg" /><br/><span>8</span></li>
             </ul>
        </div>
        <script>
            window.onload=function(){
                    var UcanSlide=function(ele,config){
                        this.config=config||{};
                        this.wrap=document.querySelector(ele);
                        this.ul_wrap=this.wrap.querySelector(‘ul‘);
                        this.ul_wrap.innerHTML+=this.ul_wrap.innerHTML;
                        this.oli=this.ul_wrap.querySelectorAll(‘li‘);
                        this.len=this.oli.length;
                        this.marginRight=this.config.marginRight||15;//设置li标签的右边距
                        this.autoScroll=this.config.autoScroll||true;//设置容器的滚动状态
                        this.scrollSpeed=this.config.scrollSpeed||60;//设置定时器时间(单位毫秒)
                        this.scrollStep=this.config.scrollSpeed||2;//设置容器滚动的步长
                        this.setTime = null;
                        this.warp_width=this.wrap.offsetWidth;
                        this.ul_wrap_width=0;
                        this.startX=0;//记录手指拖动的距离
                        this.dirX=0;//记录移动的距离
                        this.T=this;
                        this.init();
                    }
                    UcanSlide.prototype.init=function(){
                        var _this=this;
                        for(var i=0;i<this.len;i++){
                            this.oli[i].style.marginRight=this.marginRight+‘px‘;
                            this.ul_wrap_width+=(this.oli[i].offsetWidth+this.marginRight);
                        }
                        this.ul_wrap.style.width=this.ul_wrap_width+‘px‘;
                        if(this.autoScroll){
                            //执行无缝滚动
                            this.setTime=setInterval(function(){
                                _this.move();
                            },this.scrollSpeed);
                        }
                        this.ul_wrap.addEventListener(‘touchstart‘,function(e){
                            _this.touchStart(e);
                        },false);
                        this.ul_wrap.addEventListener(‘touchmove‘,function(e){
                            _this.touchMove(e);
                        },false);
                        this.ul_wrap.addEventListener(‘touchend‘,function(e){
                            _this.touchEnd(e);
                        },false);
//                        this.ul_wrap.addEventListener(‘touchstart‘,_this.touchStart,false);
//                        this.ul_wrap.addEventListener(‘touchmove‘,_this.touchMove,false);
//                        this.ul_wrap.addEventListener(‘touchend‘,_this.touchEnd,false);
                    }
                    UcanSlide.prototype.move=function(){
                        if(this.autoScroll){
                            //自由滚动状态(自右向左)
                            this.dirX=this.dirX-this.scrollStep;
                            if(this.dirX<-this.ul_wrap_width/2||this.dirX>0){
                                this.dirX=0;
                            }
                        }else{
                            //拖动状态
                            if(this.dirX<-this.ul_wrap_width/2){
                                this.dirX=0;
                            }else if(this.dirX>=0){
                                this.dirX=-this.ul_wrap_width/2;
                            }
                        }
                        this.ul_wrap.style.webkitTransform=‘translate3d(‘+this.dirX+‘px,‘+‘0px,0px)‘;
                    };
                    UcanSlide.prototype.touchStart=function(e){
                        e.preventDefault();
                        clearInterval(this.setTime);
                        this.startX=e.targetTouches[0].clientX;
                    };
                    UcanSlide.prototype.touchMove=function(e){
                        e.preventDefault();
                        this.autoScroll=false;
                        this.dirX+=e.targetTouches[0].clientX-this.startX;
                        this.startX=e.targetTouches[0].clientX;
                        this.move();
                    };
                    UcanSlide.prototype.touchEnd=function(e){
                        var _this=this;
                        this.autoScroll=true;
                        this.setTime=setInterval(function(){
                            _this.move();
                        },this.scrollSpeed);
                    };
                new UcanSlide(‘.nav-container‘);
            }
        </script>
    </body>
</html>

效果演示:

时间: 2024-08-29 10:13:15

移动端无缝滚动兼拖动插件的相关文章

JQuery实现文字无缝滚动效果 Marquee插件

推荐一个JQuery的无缝文字滚动效果,同时也可以滚动图片,也叫做跑马灯效果. 此jquery插件,依托jquery库,能实现各种滚动效果,且让HTML代码符合W3C标准. 官方演示,如下: Demo 使用方法如下: 1.加载javascript. <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript&

基于zepto的插件之移动端无缝向上滚动并上下触摸滑动

该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 公司的移动端项目是基于zepto的,有一个页面要求文字能够无缝地不停向上滚动,但查了网上的资料,大多都是基于jquery的,虽然稍作修改就可以用于移动端,但不能实现触摸上下翻滚.所以就去了zepto的官网查看其API,却发现如果要使用zepto的swipe()方法,需要引用其已经封装好的touch.js文件,我就赶紧引用了这个js文件,可在实际测试中,官网给出的touch.js文件

自己写了一个无缝滚动的插件(jQuery)

效果图: html代码: 1 <h1>无缝滚动,向右滚动</h1> 2 <ul id="guoul1"> 3 <li><img src="img/f1.jpg" alt="f1"/></li> 4 <li><img src="img/f2.jpg" alt="f2"/></li> 5 <li&g

jQuery无缝滚动插件

插件代码 ;(function ($) { // jQuery marquee 插件 $.fn.marquee = function (options) { // 默认设置 var defaults = { derection: "top", interval: 50 }; // 合并后的参数设置 var options = $.extend(defaults, options); var $mar = $(this), original = $mar.children().first

自己编写插件 之 无缝滚动

昨天 - 扒衣见君节,今天 - 牛郎织女节.光棍只能来撸代码了,哎! 首先来看下html骨架,如下: <div class="box"> <ul> <li>111</li> <li>222</li> <li>333</li> </ul> </div> 结构简单明了,没什么说的. 讲下实现原理: div box是最外层盒子,给它指定的宽高,记得给box添加一个 over

一款经典的jQuery kxbdMarquee 无缝滚动插件

<marquee> 曾是 IE 下独有的一个走马灯效果的标签,其他浏览器并不兼容,于是出现了使用 JavaScript 来模拟该效果的插件. 版本: jQuery v1.3.2+ 在线实例 实例预览 kxbdMarquee 模拟 Marquee 无缝滚动 使用方法 载入 JavaScript 文件 <script src="jquery.js"></script>  <script src="jquery.kxbdmarquee.js

liMarquee – jQuery无缝滚动插件

<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><title>jQuery无缝滚动插件liMarquee演示-向上滚动_dowebok</title><link rel="stylesheet" href="css/liMarquee.css"><style>

Javascript图片无缝滚动

js无缝滚动效果几乎在任何网页上都能看到它的身影,有的可能是使用插件,其实使用原始的javascript比较简单. 主要的是使用js位置知识. 1.innerHTML:设置或获取元素的html标签 2.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距 3.offsetWidth:设置或获取指定标签的宽度 4.setInterval():设置方法定时启动 5.clearInterval();清除定时器 效果图: 先睹为快:demo <!DOCTYPE html>

拖动插件的一些常见问题

最近项目需要,我要实现一个拖动的功能.大概的意思是:你邮箱里面的邮件列表,你可以通过鼠标mousedown后,通过鼠标移动mousemove,把特定的邮件拖动到垃圾箱啊或者草稿箱啊,如果拖动到的地方不是像垃圾箱或草稿箱的元素,就不做处理. 这个功能在邮箱项目上是很普遍的,我稍微看了下我们公司的标准邮箱的实现方法,其中有一个疑问是:为什么不用节流的方式来控制mousemove的操作.大家都知道像mousemove和scroll这种事件,你拖动一下,就可能触发很多次,导致回调方法也执行很多次,性能不