【Blogger】特定の位置にページの先頭(トップ)へ戻るボタンを常駐表示する方法

当ブログでも右下に設置しているのですが、スクロールダウンしていくと、いつでもページの先頭へ戻ることが可能だと便利です。


以下のタグを、レイアウト→ガジェットを追加→「HTML/JavaScript」内か、
</body>
より前に追加するだけで可能です。


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" >
/***********************************************
* Scroll To Top Control script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Please keep this notice intact
* Visit Project Page at http://www.dynamicdrive.com for full source code
***********************************************/
var scrolltotop={ 
    //startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control 
    //scrollto: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top). 
    setting: {startline:200, scrollto: 0, scrollduration:500, fadeduration:[500, 100]},
    controlHTML: '<img src="画像URL" />', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol" 
   controlattrs: {offsetx:10, offsety:2}, //offset of control relative to right/ bottom of window corner 
    anchorkeyword: '#top', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links
    state: {isvisible:false, shouldvisible:false},
    scrollup:function(){ 
        if (!this.cssfixedsupport) //if control is positioned using JavaScript 
            this.$control.css({opacity:0}) //hide control immediately after clicking it 
        var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto) 
        if (typeof dest=="string" && jQuery('#'+dest).length==1) //check element set by string exists 
            dest=jQuery('#'+dest).offset().top 
        else 
            dest=0 
        this.$body.animate({scrollTop: dest}, this.setting.scrollduration); 
    },
    keepfixed:function(){ 
        var $window=jQuery(window) 
        var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx 
        var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety 
        this.$control.css({left:controlx+'px', top:controly+'px'}) 
    },
    togglecontrol:function(){ 
        var scrolltop=jQuery(window).scrollTop() 
        if (!this.cssfixedsupport) 
            this.keepfixed() 
        this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false 
        if (this.state.shouldvisible && !this.state.isvisible){ 
            this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0]) 
            this.state.isvisible=true 
        } 
        else if (this.state.shouldvisible==false && this.state.isvisible){ 
            this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1]) 
            this.state.isvisible=false 
        } 
    }, 
    
    init:function(){ 
        jQuery(document).ready(function($){ 
            var mainobj=scrolltotop 
            var iebrws=document.all 
            mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode 
            mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body') 
            mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>') 
                .css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'}) 
               .attr({title:'このページの先頭へ戻る'}) 
                .click(function(){mainobj.scrollup(); return false}) 
                .appendTo('body') 
            if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text 
                mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text 
            mainobj.togglecontrol() 
            $('a[href="' + mainobj.anchorkeyword +'"]').click(function(){ 
                mainobj.scrollup() 
                return false 
            }) 
            $(window).bind('scroll resize', function(e){ 
                mainobj.togglecontrol() 
            }) 
        }) 
    } 
}
scrolltotop.init()
</script>

以下カスタマイズ用に値の説明
startline : スクロールしてからボタンが出現するまでの時間
scrollto : 戻る位置
scrollduration : 戻るときにスクロールするスピード
fadeduration : [フェードインする時間, フェードアウトする時間]
controlHTML : ボタンの画像URL
ページの先頭へ戻るボタンのアイコンダウンロードサイト
http://www.scrolltotop.com/

controlattrs : ボタンの位置{offsetx:右からの距離, offsety:下からの距離}
.attr({title:'このページの先頭へ戻る'}) : ボタンにカーソルが乗った場合に表示される文字




もっと簡単でいいという方は、同ページ内へのリンクタグ
<a href="#"></a>
を利用して下さい。



HTML
<div id="testss2">当ブログでも右下に設置しているのですが、スクロールダウンしていくと、いつでもページの先頭へ戻ることが可能だと便利です。</div>



本文



<a href="#testss2">テストトップへ戻る</a>
こうなる



テストトップへ戻る




参考サイト
http://www.jqueryrain.com/?QZvq0gPa

Related Posts

Previous
Next Post »

4 コメント

コメント
2019年6月13日 15:06 delete

どうもありがとう。できました。m(_ _)m
https://soraji1954.blogspot.com/2019/06/2019613.html

あと、bloggerのメニューを階層化(プルダウンの階層)にするにはどうしたらいいでしょうか。

Reply
avatar
バナナ
2019年10月26日 2:41 delete

すいません。
返信が遅くなってしまいました。
Bloggerのメニューを使用したことがないので、わかりませんが、代わりにHTMLガジェットを導入して手動でやるのであれば、以下の記事のコードで可能です。

https://bloggercustoman.blogspot.com/p/blog-page.html

Reply
avatar
2022年2月20日 22:23 delete

Nice information and Thank You
if you don't mind please visit my Website MPOMM
it talks about Slot Online and other fun things.
Thank You

Reply
avatar
2022年3月10日 0:02 delete

mpo4d is a good engine from mpo. This entertainment business provide by TOWERKLUB, you can do mpo4d login in here

Reply
avatar