2024年4月19日 星期五

JS / 在Blogger增加”goTop”功能

在很多網站都能夠看到

當瀏覽到頁面結尾時,會出現一個回到最上面的圖示

點擊之後,網頁就會捲到最上面 在 tricohobby也有,這是佈景主題內建的

不過在blogger就要自己土炮

參考了兩個網站的做法

網頁Top按鈕 (網頁回到頂端),純 CSS+jQuery 免圖片

參考流程設定,不過因為圖示的設定會跑掉,所以圖示的呈現方式是參考另一種方式

jQuery 筆記 - 讓網頁回到最頂點寫法

參考圖示的呈現方式,利用font-awesome載入特殊字形

而font-awesome 的詳細使用方式可以參考這個網頁

整體程式碼如下

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css"
/>
<style>
/* 56px */
.toTop-arrow {
/*width: 3.5rem;
 height: 3.5rem;
 padding: 0;
 margin: 0;
 border: 0;
 border-radius: 33%;
 opacity: 0.6;
 background: #000;*/
cursor:
pointer;
position:fixed;
right:
1rem;
bottom:
1rem;
display:
none;
}

/* arrow color */
.arrow-up {
color:
#3d82ad;
}

.arrow-footer {
color:
#cb6c8f;
}

/*arrow icon */
.fa-3x {
font-size:
5em;
}

.fas {
font-family:
"Font Awesome 5 Free";
font-weight:
900;
-moz-osx-font-smoothing:
grayscale;
-webkit-font-smoothing:
antialiased;
display:
inline-block;
font-style:
normal;
font-variant: normal;
text-rendering:
auto;
line-height:
1;
}

/*initial arrow icon */
.hide {
display:none;
}

</style>
<i id="BackTop" class="toTop-arrow fas fa-3x fa-arrow-alt-circle-up
hide"></i>
<script>
$(function(){
$('#BackTop').on("click",function(){
$('html,body').animate({scrollTop:0}, 333);
});
$(window).on( 'scroll',
function() {
//console.log('$(window).height()',
$(window).height());
//console.log('$(document).height()',
$(document).height());
//console.log('$(this).scrollTop()',
$(this).scrollTop());
//console.log('$(document).height()-$(window).height()',$(document).height()-$(window).height());
if ( $(this).scrollTop() > $(document).height()*0.10 && $(this).scrollTop() <= $(document).height()-$(window).height() - 100){
//console.log("#1");
$('#BackTop').fadeIn(222);
$('#BackTop').removeClass("arrow-footer");
$('#BackTop').addClass("arrow-up");
$('#BackTop').toggleClass("hide");
} else
if($(this).scrollTop() > $(document).height()-$(window).height() - 100 ){
//console.log("#2");
$('#BackTop').fadeIn(222);
$('#BackTop').removeClass("arrow-up");
$('#BackTop').addClass("arrow-footer");
} else {
//console.log("#3");
$('#BackTop').stop().fadeOut(222);
}
});
});
</script>

  再透過HTML/JavaScript小工具來執行

0 comments:

張貼留言