回到顶部(Top按钮)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .data{
            float: left;
            width: 150px;
            margin-left: 20px;
        }
        img{
            width: 150px;
            height: 150px;
        }
        .jia,.hua{
            display: inline-block;
        }

        .jia{
            color: red;
        }
        .more{
            width: 100px;
            height: 50px;
            margin: 20px 125px;
        }
         #all{
            width: 350px;
            height: 100%;
        }
        #
box{
            width: 350px;

        }
        .hua{
            text-decoration: line-through;
        }
         #all{
            width: 305px;
            margin: 0 auto;
        }
        .top{
            position: fixed;
            top: 350px;
            right: 0;
            width: 35px;
            height: 35px;
            background: red;
            color: white;
            border: 0;
            outline: 0;
        }
    </style>
    <script src="jquery-1.8.3.js"></script>
</head>
<body>
<div id="all">
    <div id="box">
        <!--<div class="data">
            <img src="img/1.jpg" alt=""/>
            <div class="mai">买一送一特价大放送</div>
            <div class="bao">包邮</div>
            <div class="jia">¥<span>2</span></div><div class="hua">原价&nbsp;¥<span>20</span></div>
        </div>-->
    </div>

    <button class="top">回到顶部</button>
    <button class="more">加载更多</button>
</div>
</body>
</html>

<script>
    $(".top").hide();
    $.ajax({
        url: "data.json",
        type:"get",
        success: function(data){
            var num=0;
            var seed=6;//滚动条数

                function moreTwo(){
                    var html="";
                    for(var k=num;k<num+seed&&k<data.length;k++){
                        html+="<div class='data'>"+
                        "<img src='"+data[k].imgs+"' alt=''/>"+
                                "<div class='mai'>"+data[k].title+"</div>"+
                                "<div class='bao'>包邮</div>"+
                                "<div class='jia'>¥<span>2</span></div><div class='hua'>原价&nbsp;&nbsp;&nbsp;&nbsp;¥<span>20</span></div>"+
                        "</div>";

                        //判断是否还有数据
                        if((k+1)>=data.length){
                            $(".more").html("我是有底线的");
                        }
                    }

                    $("#
box").append(html);

                }//moveTwo

            //点击加载
            $(".more").click(function(){
                    num+=seed;
                moreTwo();
            });

            moreTwo();

            //回到顶部
            $(".top").click(function(){
                $('body,html').animate({scrollTop:0},500);
            });

            //距离加载数据
            $(window).scroll(function(){
                var heightAll=$(document).height(); //获取页面长度
                var lookAll=$(window).height();//获取可视区域
                var scrAll=$(window).scrollTop();//获取滑动距离

                var hAll=heightAll+lookAll;

               //滑动加载页面的条件
                if(hAll>=scrAll){
                    num+=seed;
                 moreTwo();
                }

               //top按钮显示的条件
                if(scrAll>=350){
                    $(".top").show();
                }
            });

        }//success
    });
</script>

//json的数据 名叫data.json   (数据可添加)
[
  {"imgs":"img/1.jpg","title":"买就送","bao":"包邮","price0":"20","price1":"2"},
  {"imgs":"img/2.jpg","title":"买就送","bao":"包邮","price0":"38","price1":"1"},
  {"imgs":"img/3.jpg","title":"买就送","bao":"包邮","price0":"280","price1":"20"},
  {"imgs":"img/4.jpg","title":"买就送","bao":"包邮","price0":"270","price1":"45"}
]
根据提供的引用内容,我们可以实现一个基于Vue的一键回到顶部按钮,具体实现过程如下: 1. 在Vue组件中添加一个按钮元素,用于触发回到顶部事件。 2. 在Vue组件的data中添加一个变量isShowBackTop,用于控制回到顶部按钮的显示和隐藏。 3. 在Vue组件的methods中添加一个backTop方法,用于实现回到顶部的操作。在该方法中,我们可以使用window.scrollTo方法将页面滚动到顶部,并添加一个定时器,实现滚动的动画效果。 4. 在Vue组件的mounted生命周期函数中,添加一个滚动事件监听器,用于判断是否需要显示回到顶部按钮。当页面滚动高度大于屏幕高度时,将isShowBackTop变量设置为true,否则设置为false。 下面是一个示例代码: ```vue <template> <div> <!-- 回到顶部按钮 --> <div v-show="isShowBackTop" class="back-top" @click="backTop"> <i class="iconfont icon-top"></i> </div> <!-- 页面内容 --> <div class="content"> ... </div> </div> </template> <script> export default { data() { return { isShowBackTop: false // 控制回到顶部按钮的显示和隐藏 } }, methods: { backTop() { // 回到顶部操作 let currentScroll = document.documentElement.scrollTop || document.body.scrollTop if (currentScroll > 0) { window.requestAnimationFrame(this.backTop) window.scrollTo(0, currentScroll - (currentScroll / 5)) } } }, mounted() { // 滚动事件监听器 window.addEventListener('scroll', () => { let scrollTop = document.documentElement.scrollTop || document.body.scrollTop this.isShowBackTop = scrollTop > window.innerHeight }) } } </script> <style> .back-top { position: fixed; right: 50px; bottom: 50px; width: 50px; height: 50px; line-height: 50px; text-align: center; background-color: #ccc; border-radius: 50%; cursor: pointer; } .icon-top { font-size: 24px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值