javascript - 兩個script為什么自動換背景的不執行呢?
問題描述
前段時間表白網頁很有趣,就嘗試看看視頻寫了代碼,但是為什么背景自動更換這個script沒有執行呢?求教各位了## 兩個script只有一個實現了 ##
<!doctype html><html lang='en'> <head> <meta charset='UTF-8'> <meta name='Keywords' content=''> <meta name='Description' content=''> <title>你是我的最愛--000</title> <style type='text/css'> *{margin:0;padding:0;} #bg_body{background:url(image/1.jpg) no-repeat center #eee;background-size:cover; /* background-attachment:fixed;height:1000px; */ } /*start top*/ .top{width:400px;height:100px;margin:60px auto;font-size:30px;font-family:'華文行楷';color:#fff;} /*end top*/ /*start box*/ .box{width:310px;height:310px;margin:auto;perspective:800px;} .box .pic{position:relative;transform-style:preserve-3d;animation:play 10s linear infinite;}/*勻速 無線運行*/ .box ul li{list-style:none;position:absolute;top:0;left:0;} /*end box*/ #text{width:980px;height:130px;color:#0099ff;margin:auto;font-size:30px;font-family:'華文行楷';} /*定義一個關鍵幀*/ @keyframes play{ from{transform:rotateY(0deg);} to{transform:rotateY(360deg);} } </style> <script type='text/javascript'> window.onload=function typing (){var oBody=document.getElementById(’bg_body’);var oS=oBody.style;function BgChenge(){ oS.backgroundImage=’url(image/’+(parseInt(Math.random() * 6) + 1)+’.jpg)’;};function BgPosition(){ oS.backgroundRepeat=’no-repeat’; oS.backgroundPosition=’center’; oS.backgroundAttachment=’fixed’;};function LoadMethod(){ BgChenge(); BgPosition()};setInterval(LoadMethod,1000); }; </script></head> <body id='bg_body'> <!--start top--> <p class='top'> <marquee behavior=alternate>時光不老 我們不散</marquee> </p> <!--end top--> <!--start box--> <p class='box'> <p class='pic'> <ul> <li><img src='http://m.cgvv.com.cn/wenda/image/1.png'height=''width=''alt=''/></li> <li><img src='http://m.cgvv.com.cn/wenda/image/2.png'height=''width=''alt=''/></li> <li><img src='http://m.cgvv.com.cn/wenda/image/3.png'height=''width=''alt=''/></li> <li><img src='http://m.cgvv.com.cn/wenda/image/4.png'height=''width=''alt=''/></li> <li><img src='http://m.cgvv.com.cn/wenda/image/5.png'height=''width=''alt=''/></li> <li><img src='http://m.cgvv.com.cn/wenda/image/6.png'height=''width=''alt=''/></li> </ul> </p> </p> <!--end box--> <!--star text--> <p id='text'></p> <!--end text--> <embed src='http://m.cgvv.com.cn/wenda/music/告白氣球 - 周杰倫.mp3' Volume='30' height='0'></embed> <script src='http://m.cgvv.com.cn/wenda/js/jquery.min.js'></script> <script src='http://m.cgvv.com.cn/wenda/js/jquery.min.js'></script> <script src='http://m.cgvv.com.cn/wenda/js/jquery.snow.min.js'></script> <script> //拿到每一個li $('.pic ul li').each(function(i){var deg = 360/$('.pic ul li').size();//當前li對象 $(this).css({'transform':'rotateY('+deg*i+'deg)translateZ(216px)'}); $.fn.snow({minSize:10,maxSize:15,newOn:500,flakeColo:'#ffffff' }); });var i = 0;var str ='能夠遇見你是我最大的幸運,有了你生活變得豐富多彩,有了你,世界變得如此迷人,你是我的世界,我的世界是你,對我來說,不是在最美好的時光遇見了你,而是遇見你以后都是最美好的時光!' window.onload = function typing(){ //js 獲取p var myp = document.getElementById('text'); myp.innerHTML += str.charAt(i); i++; var id = setTimeout(typing,100); if(i==str.length){clearTimeout(id); } } </script> </body></html>
問題解答
回答1:使用 window.addEventListener(’load’,function(){})替換 window.onload = function(){}關鍵字:DOM0級事件與DOM2級事件
回答2:在js中window.onload事件是唯一的,無論你寫多少個都只執行一個,而且是最后一個,所以你可以考慮用jq中的$(function(){}),它不會因為出現的的順序而改變瀏覽器渲染,同時瀏覽器會根據$(function(){})的多少來進行渲染。
回答3:window.onload事件只能執行一次,再則你已經引用了jquery.min.js,可以嘗試改成$(function(){}),可以多次引用。
相關文章:
1. android - NavigationView 的側滑菜單中如何保存新增項(通過程序添加)2. tp5 不同控制器中的變量調用問題3. 微信小程序可以用gulp,webpack嗎?4. mysql服務無法啟動1067錯誤,誰知道正確的解決方法?5. ueditor上傳服務器提示后端配置項沒有正常加載,求助?。。。?!6. python - 為什么正常輸出中文沒有亂碼,zip函數之后出現中文編程unicode編碼的問題,我是遍歷輸出的啊。7. 老師 我是一個沒有學過php語言的準畢業生 我希望您能幫我一下8. 這段代碼既不提示錯誤也看不到結果,請老師明示錯在哪里,謝謝!9. php7.3.4中怎么開啟pdo驅動10. 提示語法錯誤語法錯誤: unexpected ’abstract’ (T_ABSTRACT)
