How jquery+css achieves the page effect of "back to top" - Qinqin Senior 318692996

The article comes from https://www.liaoxuefeng.com/article/0013738939371174a66d9fcf5094b1dbf28e9e9ccbf9d61000

back to top code

<body>
    <div class="go-top">
        <div class="arrow"></div>
        <div class="stick"></div>
    </div>
    ...
</body>

css style

div.go-top {
    display: none;
    opacity: 0.6;
    z-index: 999999;
    position: fixed;
    bottom: 113px;
    left: 90%;
    margin-left: 40px;
    border: 1px solid #a38a54;
    width: 38px;
    height: 38px;
    background-color: #eddec2;
    border-radius: 3px;
    cursor: pointer;
}

div.go-top:hover {
    opacity: 1;
    filter: alpha(opacity=100);
}

div.go-top div.arrow {
    position: absolute;
    left: 10px;
    top: -1px;
    width: 0;
    height: 0;
    border: 9px solid transparent;
    border-bottom-color: #cc3333;
}

div.go-top div.stick {
    position: absolute;
    left: 15px;
    top: 15px;
    width: 8px;
    height: 14px;
    display: block;
    background-color: #cc3333;
    -webkit-border-radius: 1px;
    -moz-border-radius: 1px;
    border-radius: 1px;
}

Use fixed positioning so that the button always appears in the lower right corner, and by setting left:90%, the button can appear on the right, but not too close to the scroll bar.

The button is invisible by default. When the page is scrolled to a certain height, the button appears. Here is implemented with jQuery. The code is quite simple:

$(function() {
    $(window).scroll(function() {
        if ($(window).scrollTop() > 1000)
            $('div.go-top').show();
        else
            $('div.go-top').hide();
    });
    $('div.go-top').click(function() {
        $('html, body').animate({scrollTop: 0}, 1000);
    });
});

Modified renderings


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449461&siteId=291194637