前端JS: 滚动事件

1. CSS样式

.scroll{
    width: 360px;
    line-height: 25px;
    overflow: hidden;/* 溢出隐藏 */
}
.height50{
    height: 50px;/* 最大高度 */
}
/* 溢出展示省略号 */
.scroll li{
    height: 25px;
    padding-left: 10px;
    width: 360px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.scroll li:hover{
    cursor: pointer;
    text-shadow: 2px 2px 6px #1F6AFB;
}
.height25 {
    height: 25px;/* 最大高度 */
}
.scrollIcon{
    line-height: 25px;height:25px;vertical-align: -4px;
}
.scrollTxt{
    line-height: 25px;height:25px;vertical-align: middle;
}

 

2.HTML

<!-- scrollIcon样式的span标签,其内容仅为图标 -->

<div class="scroll height50"  id="slideContainer1">
    <ul>
        <li>
            <span class="scrollIcon">
                <svg t="1580809997427" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1119" width="12" height="12"><path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#267CFF" p-id="1120"></path></svg></span>
            <span class="scrollTxt">资产最大值(万元:  972.3</span>
        </li>
        <li>
            <span class="scrollIcon"><svg t="1580809997427" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1119" width="12" height="12"><path d="M512 512m-448 0a448 448 0 1 0 896 0 448 448 0 1 0-896 0Z" fill="#267CFF" p-id="1120"></path></svg></span>
            <span class="scrollTxt">资产平均数(元:  100,024.76</span>
        </li>
    </ul>
</div>

 

3.JS

//引入jQuery后,编写如下滚动事件的脚本
<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);
</script>

<script type="text/javascript">
// 提示: 如需多个滚动栏,则重写上面脚本的Scroll事件即可,变量也需重命名

//绑定滚动事件
$(document).ready(function(){
    $("#slideContainer1").Scroll({
        line: 1,
        speed: 500,
        timer: 2000
    });
});
</script>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值