使用jquery实现的循环连续可停顿滚动实例


Posted in Javascript onNovember 23, 2016

使用jquery实现的循环连续可停顿滚动,并应用于滚动图片,滚动层。记得一定要有jquery库哦~

一、单行滚动

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
<meta http-equiv="Content-Type" c />   
<title>无标题文档</title>   
<mce:style type="text/css"><!--   
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}   
--></mce:style><mce:style type="text/css" mce_bogus="1"><!-- 
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;} 
--></mce:style><style type="text/css" mce_bogus="1" mce_bogus="1">ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}</style>   
<mce:script src="jquery-1[1].2.1.pack.js" mce_src="jquery-1[1].2.1.pack.js" type="text/javascript"></mce:script>   
<mce:script type="text/javascript"><!--   
function AutoScroll(obj){   
    $(obj).find("ul:first").animate({   
        marginTop:"-25px"  
    },500,function(){   
        $(this).css({marginTop:"0px"}).find("li:first").appendTo(this);   
    });   
}   
$(document).ready(function(){   
setInterval('AutoScroll("#scrollDiv")',1000)   
});   
// --></mce:script>   
</head>   
<body>   
<div id="scrollDiv">   
<ul>   
  <li>这是公告标题的第一行</li>   
  <li>这是公告标题的第二行</li>   
  <li>这是公告标题的第三行</li>   
  <li>这是公告标题的第四行</li>   
  <li>这是公告标题的第五行</li>   
  <li>这是公告标题的第六行</li>   
  <li>这是公告标题的第七行</li>   
  <li>这是公告标题的第八行</li>   
</ul>   
</div>   
</body>   
</html>

二、多行滚动 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
<title>无标题文档</title>   
<mce:style type="text/css"><!--   
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}   
--></mce:style><mce:style type="text/css" mce_bogus="1"><!-- 
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;} 
--></mce:style><style type="text/css" mce_bogus="1" mce_bogus="1">ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}</style>   
<mce:script src="jquery-1[1].2.1.pack.js" mce_src="jquery-1[1].2.1.pack.js" type="text/javascript"></mce:script>   
<mce:script type="text/javascript"><!--   
//滚动插件   
(function($){   
$.fn.extend({   
    Scroll:function(opt,callback){   
        //参数初始化   
        if(!opt) var opt={};   
        var _this=this.eq(0).find("ul:first");   
        var    lineH=_this.find("li:first").height(), //获取行高   
            line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数,默认为一屏,即父容器高度   
            speed=opt.speed?parseInt(opt.speed,10):500, //卷动速度,数值越大,速度越慢(毫秒)   
            timer=opt.timer?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒)   
        if(line==0) line=1;   
        var upHeight=0-line*lineH;   
        //滚动函数   
        scrollUp=function(){   
            _this.animate({   
                marginTop:upHeight   
            },speed,function(){   
                for(i=1;i<=line;i++){   
                    _this.find("li:first").appendTo(_this);   
                }   
                _this.css({marginTop:0});   
            });   
        }   
        //鼠标事件绑定   
        _this.hover(function(){   
            clearInterval(timerID);   
        },function(){   
            timerID=setInterval("scrollUp()",timer);   
        }).mouseout();   
    }       
})   
})(jQuery);   
$(document).ready(function(){   
    $("#scrollDiv").Scroll({line:4,speed:500,timer:1000});   
});   
// --></mce:script>   
</head>   
<body>   
<p>多行滚动演示:</p>   
<div id="scrollDiv">   
<ul>   
  <li>这是公告标题的第一行</li>   
  <li>这是公告标题的第二行</li>   
  <li>这是公告标题的第三行</li>   
  <li>这是公告标题的第四行</li>   
  <li>这是公告标题的第五行</li>   
  <li>这是公告标题的第六行</li>   
  <li>这是公告标题的第七行</li>   
  <li>这是公告标题的第八行</li>   
</ul>   
</div>   
</body>   
</html>

三、可控制向前向后的多行滚动  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml">   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />   
<title>无标题文档</title>   
<mce:style type="text/css"><!--   
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}   
--></mce:style><mce:style type="text/css" mce_bogus="1"><!-- 
ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;} 
--></mce:style><style type="text/css" mce_bogus="1" mce_bogus="1">ul,li{margin:0;padding:0}  
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}  
#scrollDiv li{height:25px;padding-left:10px;}</style>   
<mce:script src="jquery-1[1].2.1.pack.js" mce_src="jquery-1[1].2.1.pack.js" type="text/javascript"></mce:script>   
<mce:script type="text/javascript"><!--   
(function($){   
$.fn.extend({   
    Scroll:function(opt,callback){   
        //参数初始化   
        if(!opt) var opt={};   
        var _btnUp = $("#"+ opt.up);//Shawphy:向上按钮   
        var _btnDown = $("#"+ opt.down);//Shawphy:向下按钮   
        var timerID;   
        var _this=this.eq(0).find("ul:first");   
        var   lineH=_this.find("li:first").height(), //获取行高   
            line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数,默认为一屏,即父容器高度   
            speed=opt.speed?parseInt(opt.speed,10):500; //卷动速度,数值越大,速度越慢(毫秒)   
            timer=opt.timer //?parseInt(opt.timer,10):3000; //滚动的时间间隔(毫秒)   
        if(line==0) line=1;   
        var upHeight=0-line*lineH;   
        //滚动函数   
        var scrollUp=function(){   
            _btnUp.unbind("click",scrollUp); //Shawphy:取消向上按钮的函数绑定   
            _this.animate({   
                marginTop:upHeight   
            },speed,function(){   
                for(i=1;i<=line;i++){   
                    _this.find("li:first").appendTo(_this);   
                }   
                _this.css({marginTop:0});   
                _btnUp.bind("click",scrollUp); //Shawphy:绑定向上按钮的点击事件   
            });   
        }   
        //Shawphy:向下翻页函数   
        var scrollDown=function(){   
            _btnDown.unbind("click",scrollDown);   
            for(i=1;i<=line;i++){   
                _this.find("li:last").show().prependTo(_this);   
            }   
            _this.css({marginTop:upHeight});   
            _this.animate({   
                marginTop:0   
            },speed,function(){   
                _btnDown.bind("click",scrollDown);   
            });   
        }   
        //Shawphy:自动播放   
        var autoPlay = function(){   
            if(timer)timerID = window.setInterval(scrollUp,timer);   
        };   
        var autoStop = function(){   
            if(timer)window.clearInterval(timerID);   
        };   
         //鼠标事件绑定   
        _this.hover(autoStop,autoPlay).mouseout();   
        _btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);//Shawphy:向上向下鼠标事件绑定   
        _btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);   
    }      
})   
})(jQuery);   
$(document).ready(function(){   
    $("#scrollDiv").Scroll({line:4,speed:500,timer:1000,up:"btn1",down:"btn2"});   
});   
// --></mce:script>   
</head>   
<body>   
<p>多行滚动演示:</p>   
<div id="scrollDiv">   
<ul>   
  <li>这是公告标题的第一行</li>   
  <li>这是公告标题的第二行</li>   
  <li>这是公告标题的第三行</li>   
  <li>这是公告标题的第四行</li>   
  <li>这是公告标题的第五行</li>   
  <li>这是公告标题的第六行</li>   
  <li>这是公告标题的第七行</li>   
  <li>这是公告标题的第八行</li>   
</ul>   
</div>   
<span id="btn1">向前</span> <span id="btn2">向后</span>   
</body>   
</html>

以上这篇使用jquery实现的循环连续可停顿滚动实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Jquery加载时从后台读取数据绑定到dropdownList实例
Jun 09 Javascript
今天是星期几的4种JS代码写法
Sep 17 Javascript
JS,Jquery获取select,dropdownlist,checkbox下拉列表框的值(示例代码)
Jan 11 Javascript
jquery悬浮提示框完整实例
Jan 13 Javascript
Bootstrap Validator 表单验证
Jul 25 Javascript
angular.js之路由的选择方法
Sep 24 Javascript
一个简单的node.js界面实现方法
Jun 01 Javascript
jQuery滑动效果实现方法分析
Sep 05 jQuery
vue实现图片懒加载的方法分析
Feb 05 Javascript
uni-app如何页面传参数的几种方法总结
Apr 28 Javascript
Vue 自适应高度表格的实现方法
May 13 Javascript
详解Vue.js 可拖放文本框组件的使用
Mar 03 Vue.js
纯JS代码实现隔行变色鼠标移入高亮
Nov 23 #Javascript
jQuery实现ToolTip元素定位显示功能示例
Nov 23 #Javascript
详解jQuery的Cookie插件
Nov 23 #Javascript
JS 实现可停顿的垂直滚动实例代码
Nov 23 #Javascript
Dropzone.js实现文件拖拽上传功能(附源码下载)
Nov 22 #Javascript
解析jQueryEasyUI的使用
Nov 22 #Javascript
详解jQuery插件开发方式
Nov 22 #Javascript
You might like
Apache中php.ini的设置方法
2013/02/28 PHP
PHP基于SMTP协议实现邮件发送实例代码
2017/04/27 PHP
Laravel 验证码认证学习记录小结
2019/12/20 PHP
javascript检查日期格式的函数[比较全]
2008/10/17 Javascript
javascript instanceof 与typeof使用说明
2010/01/11 Javascript
基于jquery的设置页面文本框 只能输入数字的实现代码
2011/04/19 Javascript
jQuery源码分析-03构造jQuery对象-源码结构和核心函数
2011/11/14 Javascript
吐槽一下我所了解的Node.js
2014/10/08 Javascript
jquery+ajax实现注册实时验证实例详解
2015/12/08 Javascript
原生javascript实现图片无缝滚动效果
2016/02/12 Javascript
jquery表单验证插件formValidator使用方法
2016/04/01 Javascript
Bootstrap作品展示站点实战项目2
2016/10/14 Javascript
折叠菜单及选择器的运用
2017/02/03 Javascript
vue单页面应用打开新窗口显示跳转页面的实例
2018/09/21 Javascript
如何为你的JS项目添加智能提示与类型检查详解
2019/03/12 Javascript
vue2.x 通过后端接口代理,获取qq音乐api的数据示例
2019/10/30 Javascript
vue 中固定导航栏的实例代码
2019/11/01 Javascript
在vue+element ui框架里实现lodash的debounce防抖
2019/11/13 Javascript
Vue + Scss 动态切换主题颜色实现换肤的示例代码
2020/04/27 Javascript
python列表去重的二种方法
2014/02/14 Python
python随机生成指定长度密码的方法
2015/04/04 Python
编写Python脚本批量下载DesktopNexus壁纸的教程
2015/05/06 Python
Python中的模块导入和读取键盘输入的方法
2015/10/16 Python
Python字符串拼接、截取及替换方法总结分析
2016/04/13 Python
python用户评论标签匹配的解决方法
2018/05/31 Python
python 同时运行多个程序的实例
2019/01/07 Python
PyCharm搭建Spark开发环境的实现步骤
2019/09/05 Python
Python3爬虫中Selenium的用法详解
2020/07/10 Python
使用gunicorn部署django项目的问题
2020/12/30 Python
基于CSS3的CSS 多栏(Multi-column)实现瀑布流源码分享
2014/06/11 HTML / CSS
Lacoste澳大利亚官网:服装、鞋类及配饰
2018/11/14 全球购物
古驰英国官网:GUCCI英国
2020/03/07 全球购物
SQL注入攻击的种类有哪些
2013/12/30 面试题
应届生求职信写作技巧
2013/10/24 职场文书
水电工岗位职责
2014/02/12 职场文书
Windows server 2016服务器基本设置
2022/08/14 Servers