js平滑滾動到頂部,底部,指定地方


采用錨點進行頁面中的跳轉的確很方便,但是要想增加網頁的效果,可以使用jquery中的animate,實現滾動的一個動作,慢慢的滾動到你想跳轉到的位置,從而看起來會非常高大上。

滾動到頂部:

$('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);});

滾動到指定位置:

$('.scroll_a').click(function(){$('html,body').animate({scrollTop:$('.a').offset().top}, 800);});

滾動到底部:

 $('.scroll_bottom').click(function(){$('html,body').animate({scrollTop:$('.bottom').offset().top}, 800);});

源代碼:

<!DOCTYPE>
<html>
<head>
<meta charset=utf-8" />
<title>js平滑滾動到頂部、底部、指定地方</title>
<script type="text/javascript" src="http://www.daixiaorui.com/Public/js/jquery.min.js"></script>
<style>
    .box{ height:200px; width:100%; background:#ccc; margin:10px 0;}
    .location{ position:fixed; right:0; bottom:10px; width:20px; background:#FFC; padding:5px; cursor:pointer;color:#003};
</style>
</head>

<body>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box a">產品介紹產品介紹 http://www.daixiaorui.com 產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹產品介紹</div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box bottom"></div>

<div class="location">
     <p class="scroll_top">返回頂部</p>
     <p class="scroll_a">產品介紹</p>
     <p class="scroll_bottom">滑到底部</p>
</div>
<script type="text/javascript">
    jQuery(document).ready(function($){
        $('.scroll_top').click(function(){$('html,body').animate({scrollTop: '0px'}, 800);}); 
        $('.scroll_a').click(function(){$('html,body').animate({scrollTop:$('.a').offset().top}, 800);});
        $('.scroll_bottom').click(function(){$('html,body').animate({scrollTop:$('.bottom').offset().top}, 800);});
    });
</script>
</body>
</html>

文章出自:http://www.daixiaorui.com/read/92.html ,只為收藏學習!謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM