添加浮動按鈕點擊滾動到網頁底部的純JavaScript示範代碼 IE9、11,Maxthon 1.6.7,Firefox30、31,360極速瀏覽器7.5.3.308下測試正常,firefox30

來源:互聯網
上載者:User

添加浮動按鈕點擊滾動到網頁底部的純JavaScript示範代碼 IE9、11,Maxthon 1.6.7,Firefox30、31,360極速瀏覽器7.5.3.308下測試正常,firefox30

  這兩天想在網頁中添加一個添加浮動按鈕,點擊該按鈕則滾動到網頁底部。在網上bing搜尋了一下,大多是JQuery的。

  我想要純JavaScript的,只好DIY了。在IE9、11,Maxthon 1.6.7,Firefox30、31,360極速瀏覽器7.5.3.308下測試正常。

  其中痛點在於,setScrollBottom這個函數。

  按常規寫法:

function setScrollBottom(value) {if (document.documentElement.scrollTop){ document.documentElement.scrollTop += value;} else {document.body.scrollTop += value;}}


 

  在網頁第一次載入完成時點擊浮動按鈕,在IE9、11,Maxthon 1.6.7,Firefox30、31中會無效,在360極速瀏覽器則有效。


  在IE9、11,Maxthon 1.6.7,Firefox30、31中無效,是因為此時捲軸按鈕在頂部,不論document.body.scrollTop還是document.documentElement.scrollTop值都是0,document.documentElement.scrollTop += value;不會被執行,而document.body.scrollTop += value這句雖然會被執行,但由於聲明了文件類型,document.body.scrollTop恒為0。


  在360極速瀏覽器中有效,則是因為即使聲明了文件類型,chrome核心仍然不認document.documentElement.scrollTop,只用document.body.scrollTop。

 

  如果改成:

function setScrollBottom(value) {if (document.body.scrollTop){ document.body.scrollTop += value;} else { document.documentElement.scrollTop+= value;}}


  效果就會反過來。

 

  要想兩全齊美,當然我們可以通過添加識別瀏覽器類型的代碼來進行區分並做相應處理,不過我們可以利用document.body.scrollTop值是否恒為0這一點來做簡單的區分,如下面的示範代碼所示。

 

  btw,在網上找到兩個直接沉底的簡單代碼:

  其1:

<a href="#1" onclick="window.scrollTo(0,99999);return false;">跳到網頁底部方法1</a>


  思路:document.all[document.all.length-1]獲得最後一個元素。scrollIntoView()滾動到視線內。IE和FireFox都行。文檔最末元素不在最底下時不適用。

  其2:

<a href="#1" onclick="window.scrollTo(0,99999);return false;">跳到網頁底部方法2</a>


  思路:只要滾動到一個很遠很遠的地方就可以了。

 

  也一併放入示範代碼裡了。

 

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn"><HEAD><TITLE>添加浮動按鈕點擊滾動到網頁底部的純JavaScript示範代碼(goBottom)by PurpleEndurer</TITLE><style type="text/css">#goBottomBtn{float:right; POSITION: fixed; TEXT-ALIGN: center; LINE-HEIGHT: 20px; WIDTH: 30px; BOTTOM: 65px; FONT-SIZE: 12px; CURSOR: pointer; RIGHT: 0px; _position: absolute; _right: auto; border: 1px solid gray; background:green; color:white;}</style></HEAD><BODY><p>添加浮動按鈕點擊滾動到網頁底部的純JavaScript示範代碼(goBottom) by PurpleEndurer <a href="http://blog.csdn.net/purpleendurer">http://blog.csdn.net/purpleendurer</a></p><p>IE9、11,Maxthon 1.6.7,Firefox30、31,360極速瀏覽器7.5.3.308下測試正常</p><p><a href="#1" onclick="window.scrollTo(0,99999);return false;">跳到網頁底部方法1</a>  <a href="#1" onclick="window.scrollTo(0,99999);return false;">跳到網頁底部方法2</a></p><script type="text/javascript"><!--//添加99行文字function writeLine(){for (var j=0; j < 100; j++){document.writeln('<p>'+ j +' 添加浮動按鈕點擊滾動到網頁底部的純JavaScript示範代碼<p>');}}writeLine();function goBottomEx(){function addGoBottomDiv(){var div = document.createElement('div');div.innerHTML = '<br /><span style="font-size:30px;" title="直達底部">↓</span>';div.setAttribute('id', 'goBottomBtn');document.body.appendChild(div);}var o = document.getElementById('goBottomBtn');if (null==o){addGoBottomDiv();showBottomBtn();}o = document.getElementById("goBottomBtn");function getScrollTop(){return document.documentElement.scrollTop || document.body.scrollTop;}function setScrollBottom(value) {document.body.scrollTop += value;if (0==document.body.scrollTop){ document.documentElement.scrollTop += value;}}function isBottom() {var a = document.documentElement.clientHeight || document.body.clientHeight;var b = document.documentElement.scrollTop || document.body.scrollTop; var c = document.documentElement.scrollHeight || document.body.scrollHeight;return (a+b >= c);}function showBottomBtn(){var o = document.getElementById('goBottomBtn');o.style.display = isBottom() ?  "none" : "";}window.onscroll = function() {showBottomBtn();}o.onmousemove = function() {this.style.background = "#33cc66";}o.onmouseout = function() {this.style.background = "green";}o.onclick = function() {var goBottom = setInterval(scrollMove, 10);function scrollMove() {setScrollBottom((getScrollTop() + 100) / 5); //網頁剛載入完成時getScrollTop()傳回值為0if (isBottom()) {clearInterval(goBottom);}}//scrollMove()}//o.onclick}//goBottomEx()goBottomEx()//--></script></BODY></HTML>



 


網頁 選中選擇框內容,點擊添加按鈕,把內容添加到右邊選擇框,怎使用javascript實現??急

下面的例子在chrome22 IE9 firefox16下測試通過
<select size="5" id="select1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="button" name="button" id="button" value="提交" onClick="addOption()">
<input type="button" name="button" id="button" value="刪除" onClick="delOption()">
<select id="select2" size="5"></select>
<script>

function addOption(){
var index=select1.selectedIndex;
if(index<0){
alert("沒有選中要添加的項目")
return false
}
var theOpt=select1.options[index]
//取當前選中的元素
,txt=theOpt.text
,val=theOpt.value
//擷取text、value值
,newOpt=new Option(txt,val)
//建立新的元素
,len=select2.options.length
//得到目標所有元素的數量
select2.options[len]=newOpt
//賦值完成元素的添加
}
function delOption(){
var index=select2.selectedIndex;
if(index<0){
alert("沒有選中要刪除的項目")
return false
}
select2.remove(index)
}
</script>...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.