jQuery实现数据循环滚动

用jQuery简单实现了数据循环滚动效果,鼠标移上去滚动停止,鼠标移开继续滚动。如果承装数据的父盒子的高度大于数据的高度,则不滚动显示。效果图如下。前面要引入jquery。
在这里插入图片描述
css如下:

<style>
    .bigbox {
        border: 1px solid #E9F2FA;
        width: 500px;
        background-color: #101129;
    }
    .mybox {
        width: 500px;
        line-height: 28px;
        font-size: 16px;
        height: 340px;
        overflow: hidden;
        font-size: 12px;
        box-shadow: 2px #68D8FE;
    }
    .head {
        width: 500px;
        display: flex;
        justify-content: space-between;
        height: 50px;
        background-color: #27283E;
        color: #68D8FE;
        font-size: 14px;
    }
    .head p {
        width: 130px;
        text-align: center;
        height: 20px;
        line-height: 20px;
    }
    div li {
        list-style: none;
        display: flex;
        justify-content: space-between;
        color: #61A8FF;
    }
    div li:hover {
        cursor: pointer;
        color: #3EDEF9;
        background-color: #27283E;
    }
    div li p {
        width: 130px;
        text-align: center;
        height: 14px;
        line-height: 14px;
    }
    .aa {
        color: #EFEA3B;
        width: 1px;
        height: 1px;
        position: absolute;
        top: 5px;
        left: 2px;
    }
</style>

html:

 <div class="bigbox">
        <div class="head">
            <p>开发项目</p>
            <p>房屋地址</p>
            <p>预计税费</p>
            <p>纳税人员</p>
        </div>
        <div class="mybox">
            <div class="add" style="margin-top: -1px;">
                //ajax后台取出数据循环渲染
            </div>
        </div>
    </div>

js:
主要的核心思想是,当margin-top的绝对值大于等于line-height时,将margin-top置0,然后截取此条数据拼接到末尾,以此达到循环滚动的效果。

<script>
        (function ($) {
            $.fn.myscroll = function (detail) {
                var rule = detail;
                var rowheight = rule["rowHeight"]
                var speed = rule["speed"]
                var that = $(this);//mybox
                var time = setInterval(function () {
                    if (that.find("div").height() <= that.height()) {
                        clearInterval(time);
                    } else {
                        marquee(that, rowheight);
                    }
                }, speed);

                that.hover(function () {
                    clearInterval(time);
                    $(this).find('div').each(function (i) {
                        $(this).click(function () {
                            //这里处理跳转事件
                            var jumpid = $(this).attr('id');//data的id
                            window.location.href = 'index.html';
                        })
                    })
                }, function () {
                    time = setInterval(function () {
                        if (that.find("div").height() <= that.height()) {
                            clearInterval(time);
                        } else {
                            marquee(that, rowheight);
                        }
                    }, speed);
                });
                function marquee(obj, rowheight) {
                    obj.find("div").animate({ marginTop: '-=1' }, 0, function () {
                        var s = Math.abs(parseInt($(this).css("margin-top")));
                        if (s >= rowheight) {
                            //这里this是ul,截取第一条拼接在最后一条 达到循环播放的效果
                            $(this).find("li").slice(0, 1).appendTo($(this));
                            $(this).css("margin-top", 0);
                        }
                    });
                }
            }
        })(jQuery);

        $(document).ready(function () {
            $.ajax({
                type: "get",
                contentType: "application/json;charset=UTF-8",
                url: "http://192.168.11.104:8080/project/list",
                success: function (result) {
                    for (let i = 0; i < result.data.length; i++) {
                        $('.add').append("<li id='" + result.data[i].id + "'><p>" + result.data[i].project + "</p><p>" + result.data[i].address + "</p><p>" + result.data[i].money + "</p><p>" + result.data[i].person + "</p></li>")
                    }
                    $("div.mybox").myscroll({
                        speed: 80, 
                        rowHeight: 38 
                    });
                },
            });
        })
    </script>
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用jQuery的animate()方法和定时器实现无限循环滚动。具体步骤如下: 1. 将需要滚动的元素复制一份,并插入到元素列表的末尾,形成一个循环列表。 2. 使用定时器,每隔一定时间执行一次滚动动画。 3. 在滚动动画完成后,判断当前滚动到的位置是否是复制元素的位置,如果是,则立即将滚动位置重置到原始位置。 下面是一个简单的示例代码: HTML代码: ``` <div class="scroll-wrap"> <ul class="scroll-list"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> ``` CSS代码: ``` .scroll-wrap { overflow: hidden; } .scroll-list { width: 5000px; /* 滚动列表的总宽度,需要根据实际情况进行调整 */ position: relative; left: 0; margin: 0; padding: 0; list-style: none; } .scroll-list li { float: left; width: 200px; /* 每个元素的宽度,需要根据实际情况进行调整 */ height: 100px; /* 每个元素的高度,需要根据实际情况进行调整 */ text-align: center; line-height: 100px; font-size: 36px; background-color: #ccc; margin-right: 20px; /* 每个元素之间的间距,需要根据实际情况进行调整 */ } ``` JavaScript代码: ``` $(function() { var $scrollWrap = $('.scroll-wrap'); var $scrollList = $('.scroll-list'); var $scrollItems = $scrollList.find('li'); var scrollWidth = $scrollItems.eq(0).outerWidth() * $scrollItems.length; $scrollList.css({ width: scrollWidth }); // 将列表复制一份,并插入到末尾 $scrollList.append($scrollItems.clone()); // 定时器,每隔1秒执行一次滚动动画 setInterval(function() { $scrollList.animate({ left: '-=200px' }, 500, function() { // 判断是否滚动到了复制元素的位置 if ($scrollList.position().left <= -scrollWidth) { $scrollList.css({ left: 0 }); } }); }, 1000); }); ``` 在上面的代码中,我们通过计算滚动列表的总宽度,并将滚动元素复制一份,形成一个循环列表。然后使用定时器每隔一定时间执行一次滚动动画,滚动完成后判断是否到达复制元素的位置,如果是则重置滚动位置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值