文字上下滚动特效

这段代码展示了如何使用jQuery扩展方法实现文字上下滚动的特效。通过定义滚动速度、滚动行数和间隔时间,可以自定义滚动效果。当鼠标悬停在元素上时,滚动会暂停,离开后继续滚动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、文字上下滚动特效

<head>
    <title></title>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <style type="text/css">
        .list {
            height: 100px;
            text-overflow-y: hidden;
            overflow: hidden;
            width: 300px;
            border: 1px solid #000;
            padding:0 5px;
        }
        .active{background:#b00;color:#fff;}
    </style>
</head>
<body>
<div id="list">
    <div>生活, 一半是回忆, 一半是继续~~</div>
    <div>对自己苛刻,疲##惫....从来不想责怪别人,只想做好自己,也从来都是我的不对。</div>
    <div>前进、前进、再前进!哈哈</div>
    <div>人生如戏~!何必在意...</div>
    <div>找份开心的工作过早上8点到晚上18点,找个喜欢的女人过晚上18点到早上8点。这就是生活</div>
    <div>宁可高傲的发霉,也不低调的凑合。我们,那被逼出来的成熟~</div>
</div>
<script type="text/javascript">
    var $ = $ || window;
    $.get = function (expr) {
        function clsMatcher(className){
            return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
        }
        var e = typeof expr == "string" ? document.getElementById(expr) : expr;
        var elem = {
            dom:e,
            addClass:function(className) {
                var oldCls = e.className;
                e.className = !oldCls ?
                        className :
                        clsMatcher(className).test(oldCls) ?
                                oldCls : [oldCls,className].join(" ");
                return this;
            },remove:function() {
                e.parentNode && e.parentNode.removeChild(e);
                return this;
            },removeClass:function(className){
                e.className =(e.className||"").replace(clsMatcher(className),"$1$2");
                return this;
            },on:function(type, callback) {
                if (window.attachEvent)
                    e.attachEvent("on" + type, callback);
                else
                    e.addEventListener(type, callback, false);
                return this;
            },first:function(type) {
                var children = e.childNodes;
                for (var k = 0; k < children.length; k++) {
                    if (children[k].nodeType == 3)
                        continue;
                    if (!type || children[k].tagName.toLowerCase() == type.toLowerCase())
                        return $.get(children[k]);
                }
            },appendTo:function(parent){
                var p=parent.dom||parent;
                p.appendChild(e);
                return this;
            }
        };
        var directions = ["Left","Top"];
        for (var i = 0; i < directions.length; i++) {
            var method = "scroll" + directions[i];
            elem[method] = function(val) {
                return arguments.length ? e[method] = val : e[method];
            };
        }
        var sides = ["Width","Height"];
        for (var i = 0; i < sides.length; i++) {
            var side = sides[i];
            elem[side.toLowerCase()] = function(val) {
                if (arguments.length) {
                    e.style[side.toLowerCase()] = val + "px";
                    return this;
                }
                return  e["offset" + side] || e[side];
            }
        }
        return elem;
    };
    function Timer(c) {
        var thread;
        var delay = c.delay || 200;
        var runner = c.run || typeof c == "function" ? c : function() {
        };
        this.start = function() {
            !thread && (thread = window.setInterval(runner, delay));
            return this;
        }
        this.stop = function() {
            var snapshot = thread;
            thread = null;
            snapshot && window.clearInterval(snapshot);
            return this;
        }

    }
    var list = $.get("list").addClass("list");
    var increment = 5;
    var timer = new Timer(function() {
        var offset = list.scrollTop();
        var first = list.first().addClass('active');
        if (offset >= first.height()) {
            list.first().removeClass('active').remove().appendTo(list);
            list.scrollTop(increment);
        } else {
            list.scrollTop(Math.min(offset + increment,first.height()));
        }
    });
    timer.start();
    list.on("mouseover", timer.stop).on("mouseout", timer.start);

</script>


特效二:

<style type="text/css">
*{margin:0;padding:0;}
ul,li{list-style-type:none;}
</style>
<script type="text/javascript">
//滚动插件
(function($){
    $.fn.extend({
        Scroll:function(ss,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):3000, //卷动速度,数值越大,速度越慢(毫秒)
                    timer=opt.timer?parseInt(opt.timer,10):6000; //滚动的时间间隔(毫秒)
                if(line==0) line=1;
                var upHeight=0-line*lineH;
                if(ss == 1){
                    //滚动函数
                    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();
                }else if(ss == 2){
                    //滚动函数
                    scrollUp1=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("scrollUp1()",timer);
                    }).mouseout();
                }
        }

    });

})(jQuery);


$(document).ready(function(){
    $("#scrollDt").Scroll(2,{line:1,speed:1000,timer:5000});
    $("#scrollDiv").Scroll(1,{line:1,speed:400,timer:3000});
});


<div class="hotitem" id="scrollDiv" style="height:196px;min-height:25px;line-height:25px;overflow:hidden;">
                    <ul class="tuwenul">
                       <s:iterator value="produceLists">
                         <li class="yixin">
                             <div class="bankicon">
                                 <img src="${ctx}/member/shop/showInstitutionLogo.do?institutionId=<s:property value='feLspImManager.feLspImInstitutionId'/>" width="32" height="32" />
                             </div>
                               <div class="name"><s:property value="produceName"/></div>
                               <div class="tiaojian"><span style="color:#F9692C;"><s:property value="productSlogan"/></span></div>
                               <input name="" type="button" onclick="xianshi('<s:property value="feLspImProductId" />')" />
                         </li>
                       </s:iterator>
                    </ul>s
            </div>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值