简单实现纵向无缝滚动(不要忘记引入jquery文件哦)
看效果:

1、HTML代码
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="utf-8">
- <title>简单的jQuery无缝向上滚动效果</title>
- </head>
- <body>
- <div class="myscroll">
- <ul>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- <li><a href="">简单的jQuery无缝向上滚动效果</a></li>
- </ul>
- </div>
- </body>
- </html>
2、css代码
- <style>
- * { margin: 0; padding: 0;list-style:none;}
- .myscroll {
- width: 300px;
- height: 260px;
- margin: 0 auto;
- line-height: 26px;
- font-size: 12px;
- overflow: hidden;
- border:2px solid orange;
- }
- .myscroll li {
- height: 26px;
- padding:0 10px;
- font-size:14px;
- }
- .myscroll a {
- color: #333;
- text-decoration: none;
- }
- .myscroll a:hover {
- color: orange;
- text-decoration: underline;
- }
- </style>
3、js代码
- (function($){
- $.fn.myScroll = function(options){
- //默认配置
- var defaults = {
- speed:40, //滚动速度,值越大速度越慢
- rowHeight:24 //每行的高度
- };
-
- var opts = $.extend({}, defaults, options),
- intId = [];
-
- function marquee(obj, step){
-
- obj.find("ul").animate({//html中必须有的ul
- marginTop: '-=1'
- },0,function(){
- var s = Math.abs(parseInt($(this).css("margin-top")));
- if(s >= step){
- $(this).find("li").slice(0, 1).appendTo($(this));//截取ul中的第一个li,添加到ul的最后
- $(this).css("margin-top", 0);
- }
- });
- }
-
- this.each(function(i){
- var rowHeight = opts["rowHeight"],
- speed = opts["speed"],
- _this = $(this);//这里的_this指向div.myscroll
-
- intId[i] = setInterval(function(){
- if(_this.find("ul").height()<=_this.height()){//当ul的高度小于html中,div.myscroll的高度,则结束定时器
- clearInterval(intId[i]);
- }else{
- marquee(_this, rowHeight);
- }
- }, speed);
-
- _this.hover(function(){//鼠标移动到div.myscroll上时,结束定时器
- clearInterval(intId[i]);
- },function(){//鼠标离开div.myscroll容器,判断ul的高度若小于等于div.myscroll高度,则结束定时器(不滚动),否则调用marquee函数
- intId[i] = setInterval(function(){
- if(_this.find("ul").height()<=_this.height()){
- clearInterval(intId[i]);
- }else{
- marquee(_this, rowHeight);
- }
- }, speed);
- });
- });
- }
- })(jQuery);
4、调用
- $(function(){
- $('.myscroll').myScroll({
- speed: 40, //数值越大,速度越慢
- rowHeight: 26 //li的高度
- });
- });
以上所述是小编给大家介绍的jquery简单实现纵向的无缝滚动详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对w3xue网站的支持!