回到顶部按钮

这个回到顶部按钮大家到处都可以看到,今天终于自己做了一个,分享给大家

css样式

<style type="text/css">
#go_top {
            display: none;
            position: fixed;
            width: 40px;
            bottom: 25px;
            padding: 10px 8px;
            color: #666;
            background: #ECECEC;
            cursor: pointer;
            font: 24px/12px Helvetica,Arial,Verdana,sans-serif;
            opacity: 0.7;
            outline: 0 none;
            text-align: center;
            text-decoration: none;
            text-shadow: 0 0 1px #DDD;
            vertical-align: baseline;
            -moz-border-radius: 2px;
            -webkit-border-radius: 2px;
            border-radius: 2px;
        }
</style>

jquery代码

<script>
$(document).ready(function(){
        $(function () {
            //检测屏幕高度
            var height=$('.row-fluid').offset().top;
            //scroll() 方法为滚动事件
            $(window).scroll(function(){
                if ($(window).scrollTop()>height){
                    $('#go_top').show();
                }else{
                    $('#go_top').hide();
                }
            });
            $("#go_top").click(function(){
                $('body,html').animate({scrollTop:0},100);
                return false;
            });
        });
    });
</script>

html部分

<div id="go_top" style="right:20px;">
        <p></p>
</div>

效果图

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以实现一个基于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、付费专栏及课程。

余额充值