ibeautiful
2022-04-03 17:13:12
<a href="#" onClick="click_scroll();">滾動</a>上面這個是一個a標簽,點擊后可以滾動到頁面指定的位置,這是通過jquery的動畫取巧實現的,代碼是可以執行的<script type="text/javascript">function click_scroll() {var scroll_offset = $(“#here”).offset();$("body,html").animate({scrollTop:scroll_offset.top},300);}</script><div style="height:500px" ></div><div id="here">點擊后滾動到這里</div>以上代碼可以運行那么下面問題來了如果有多個按鈕(a標簽)我想滾動到別的位置,該怎么辦,一開始我是這么想的:<a href="#" onClick="click_scroll(#here1);">滾動到here1</a><a href="#" onClick="click_scroll(#here2);">滾動到here2</a><script type="text/javascript">function click_scroll(a) {var e = $(a);var scroll_offset = $(a).offset();$("body,html").animate({scrollTop:scroll_offset.top},300);}</script><div style="height:500px" ></div><div id="here1">點擊后滾動到here1</div><div id="here2">點擊后滾動到here2</div>后來發現,我想多了,各大神,在嗎,幫幫我~~~該怎么做
2 回答
Helenr
TA貢獻1780條經驗 獲得超4個贊
<a href="#" onClick="click_scroll('here1');">滾動到here1</a>
<a href="#" onClick="click_scroll('here2');">滾動到here2</a>
<script type="text/javascript">
function click_scroll(a) {
var e = $('#'+a);
var scroll_offset = e.offset();
$("body,html").animate({scrollTop:scroll_offset.top},300);
}
</script>
<div style="height:500px" ></div>
<div id="here1">點擊后滾動到here1</div>
<div id="here2">點擊后滾動到here2</div>
天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
無論你怎么來實現多個滾動效果,你body的滾動條只有一個,真正的滾動到的位置是最后一個滾動的。前面的會依次被后面的給覆蓋掉。
就好比你定義變量var a=1;你需要吧a賦值成a=2,然后再賦值成a=3;那最后a的值就是3......
添加回答
舉報
0/150
提交
取消
