首页 > 其他 > 详细

底部置顶效果

时间:2020-03-25 09:48:41      阅读:59      评论:0      收藏:0      [点我收藏+]
<template>
    <div id="app">
        <v-header></v-header>
        <div>
            <router-view />
        </div>
        <v-footer></v-footer>
        <div v-show="isGotop" @click="goTop">
            <i class="el-icon-caret-top goTop"></i>
        </div>
        
    </div>
</template>

<script>
    export default {
        name: App,
        data() {
            return {
                isGotop: false
            }

        },
        created() {
            this.listenerFunction();
        },
        beforeDestroy() {
            document.removeEventListener("scroll", this.listenerFunction);
            let vm = this
            clearInterval(vm.topTime)
        },
        methods: {
            goTop() {
                let vm = this
                vm.topTime = setInterval(function() {
                    if (document.body.scrollTop) {
                        document.body.scrollTop -= 10
                        if (document.body.scrollTop <= 0) {
                            document.body.scrollTop = 0
                            clearInterval(vm.topTime)
                        }
                    } else {
                        document.documentElement.scrollTop -= 10
                        if (document.documentElement.scrollTop <= 0) {
                            document.documentElement.scrollTop = 0
                            clearInterval(vm.topTime)
                        }
                    }
                }, 20)
            },
            listenerFunction(e) {
                document.addEventListener(scroll, this.handleScroll, true);
            },
            handleScroll() {
                var bodyHeight = document.documentElement.clientHeight //可视区
                var ViewHeight = this.getViewPort().height
                var documentHeight = this.getDocumentPort().height
                var scrollTop = this.getScrollTop().height
                var top = ViewHeight + scrollTop
                if (top > Math.round(documentHeight / 2) && scrollTop !== 0) {
                    this.isGotop = true
                } else {
                    this.isGotop = false
                }
            },
            getScrollTop() {
                if (window.pageYOffset !== undefined) {
                    return {
                        height: window.pageYOffset
                    }
                } else {
                    return {
                        height: document.documentElement.scrollTop
                    }
                }
            },

            // *视口的大小,部分移动设备浏览器对innerWidth的兼容性不好,需要
            //  *document.documentElement.clientWidth或者document.body.clientWidth
            //  *来兼容(混杂模式下对document.documentElement.clientWidth不支持)。
            //  *使用方法 : getViewPort().width;

            getViewPort() {
                if (document.compatMode == "BackCompat") { //浏览器嗅探,混杂模式
                    return {
                        width: document.body.clientWidth,
                        height: document.body.clientHeight
                    };
                } else {
                    return {
                        width: document.documentElement.clientWidth,
                        height: document.documentElement.clientHeight
                    };
                }
            },
            getDocumentPort() {
                if (document.compatMode == "BackCompat") {
                    return {
                        width: document.body.scrollWidth,
                        height: document.body.scrollHeight
                    };
                } else {
                    return {
                        width: Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth),
                        height: Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight)
                    }
                }
            }
        }
    }
</script>

<style>
    #app {
        font-family: ‘Avenir‘, Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        margin: 0;
        padding: 0;
        background-color: #fbfbfb;
    }
</style>
<style lang="less" scoped>
    .goTop {
        font-size: 50px;
        color: #1769D9;
        position: fixed;
        bottom: 100px;
    }

    @media screen and (max-width: 1366px) {
        .goTop {
            right: 10px;
        }
    }

    @media screen and (min-width: 1366px) {
        .goTop {
            right: 10px;
        }
    }

    @media screen and (min-width: 1440px) {
        .goTop {
            right: 40px;
        }
    }

    @media screen and (min-width: 1680px) {
        .goTop {
            right: 180px;
        }
    }

    @media screen and (min-width: 1920px) {
        .goTop {
            right: 240px;
        }
    }
</style>

 

底部置顶效果

原文:https://www.cnblogs.com/dongrr7/p/12563470.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!