[6][A.Thông điệp][slider-top-big][nothing]
You are here: Home / , , Thêm nút trượt đầu trang, cuối trang cho web hay blog

Thêm nút trượt đầu trang, cuối trang cho web hay blog

| No comment
Thêm nút trượt đầu trang, cuối trang cho web hay blog



Có rất nhiều trang có button trượt đầu trang hay cuối trang để tiện cho người đọc.


Hôm nay, mình sẽ giới thiệu với mọi người cách làm 2 button up, down này. (cuộn về đầu trang hoặc cuối trang).

Cách 1: Tham khảo từ zlatanblog.

Thêm CSS (Add trước   ]]></b:skin>  với blogger)


#go_top{ display:block; width:31px; height:31px; position:fixed; background-color:#2ccaae; bottom:15px; right:15px;}
/*-- tạo mũi tên --*/#go_top:before{ content:""; position:Absolute; width:0; height:0; border:11px solid transparent; border-bottom:11px solid white; left:5px; top:-8px;}
#go_top:after{ content:""; position:Absolute; width:10px; height:17px; background-color:white; left:11px; top:14px;}


Add Jquery trước   </head> hoặc sau <head>. (Kiểm tra trong code nếu đã có thì bỏ qua)

 <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>



Add code này trong phần body của trang web hay blog của bạn

<script type="text/javascript">
<a href="#" id="go_top"></a>(funcion(){ // Cuộn trang lên với scrollTop $('#go_top').click(function(){ $('body,html').animate({scrollTop:0},400); return false; })})(jQuery)
/ Ẩn hiện icon go-top$(window).scroll(function(){ if( $(window).scrollTop() == 0 ) { $('#go_top').stop(false,true).fadeOut(600); }else{ $('#go_top').stop(false,true).fadeIn(600); }});
</script>

 Cách 2: Với cách này thì có cả button down.

Thao tác Add vào blog cũng như cách 1.



Đầu tiên cũng cần Add CSS trước:

<style type='text/css'>
      .nav_up{
      padding:7px;
      background-color:white;
      border:1px solid #CCC;
      position:fixed;
      background:transparent url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgoA8-5JtVGfFKxks3tFh-GAPURUC-asJO3eN5VhpbwOy1u-h2lnQeLZhEUVO_qedBattdgjEUKgBbdOk25Cm9BA7C-GH17kluYQiY1_Ay28AcqbvzYRsWo49KW5WAzg2mshHhkFQWbOxk/s16/arrow_up.png) no-repeat top left;
      background-position:50% 50%;
      width:20px;
      height:20px;
      opacity:0.7;
      white-space:nowrap;
      cursor: pointer;
      -moz-border-radius: 3px 3px 3px 3px;
      -webkit-border-top-left-radius:3px;
      -webkit-border-top-right-radius:3px;
      -khtml-border-top-left-radius:3px;
      -khtml-border-top-right-radius:3px;
      filter:progid:DXImageTransform.Microsoft.Alpha(opacity=70);
      top:600px;
      right:100px;
      }
      .nav_down{
      padding:7px;
      background-color:white;
      border:1px solid #CCC;
      position:fixed;
      background:transparent url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizT1beZW4kwZA-lSdV5D9bhghNCOjhYmpgfst9EQVPlG6pYcz5VrBs9Gn8Op-9_lwKIQ3lOTk3tW5t8-_iSdZOVN40ufytAyZhlu4mu29Tr7ZtS42mc2OWZiDd9dFZlRTsgJam_DPP2vM/s16/arrow_down.png) no-repeat top left;
      background-position:50% 50%;
      width:20px;
      height:20px;
      top:600px;
      right:50px;
      opacity:0.7;
      white-space:nowrap;
      cursor: pointer;
      -moz-border-radius: 3px 3px 3px 3px;
      -webkit-border-top-left-radius:3px;
      -webkit-border-top-right-radius:3px;
      -khtml-border-top-left-radius:3px;
      -khtml-border-top-right-radius:3px;
      filter:progid:DXImageTransform.Microsoft.Alpha(opacity=70);
      }
    </style>



Thêm đoạn code này vào trước </head> hay trong <body> đều được


    <script src='http://myhostingjs.googlecode.com/files/scroll-startstop.events.jquery.js' type='text/javascript'/>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script>
      $(function() {
      var $elem = $(&#39;#noidung&#39;);
      $(&#39;#nav_up&#39;).fadeIn(&#39;slow&#39;);
      $(&#39;#nav_down&#39;).fadeIn(&#39;slow&#39;);
      $(window).bind(&#39;scrollstart&#39;, function(){
      $(&#39;#nav_up,#nav_down&#39;).stop().animate({&#39;opacity&#39;:&#39;0.2&#39;});
      });
      $(window).bind(&#39;scrollstop&#39;, function(){
      $(&#39;#nav_up,#nav_down&#39;).stop().animate({&#39;opacity&#39;:&#39;1&#39;});
      });
      $(&#39;#nav_down&#39;).click(
      function (e) {
      $(&#39;html, body&#39;).animate({scrollTop: $elem.height()}, 800);
      }
      );
      $(&#39;#nav_up&#39;).click(
      function (e) {
      $(&#39;html, body&#39;).animate({scrollTop: &#39;0px&#39;}, 800);
      }
      );
      });
    </script>
Cuối cùng là add code html này trong body


<div style="display:none;" class="nav_up" id="nav_up"></div>
<div style="display:none;" class="nav_down" id="nav_down"></div>

Chú ý: Bạn nên lưu lại tất cả các file js vào host của bạn để đảm bảo dữ liệu.

Cảm ơn bạn đã quan tâm bài viết. Nếu bạn không làm được thì có thể để lại comment, mình sẽ hướng dẫn.