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://www.aoyou183.cn/wenda/image/1.png'height=''width=''alt=''/></li> <li><img src='http://www.aoyou183.cn/wenda/image/2.png'height=''width=''alt=''/></li> <li><img src='http://www.aoyou183.cn/wenda/image/3.png'height=''width=''alt=''/></li> <li><img src='http://www.aoyou183.cn/wenda/image/4.png'height=''width=''alt=''/></li> <li><img src='http://www.aoyou183.cn/wenda/image/5.png'height=''width=''alt=''/></li> <li><img src='http://www.aoyou183.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://www.aoyou183.cn/wenda/music/告白氣球 - 周杰倫.mp3' Volume='30' height='0'></embed> <script src='http://www.aoyou183.cn/wenda/js/jquery.min.js'></script> <script src='http://www.aoyou183.cn/wenda/js/jquery.min.js'></script> <script src='http://www.aoyou183.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-studio - Android Studio 運行項目的時候一堆警告,跑步起來!?2. dockerfile - [docker build image失敗- npm install]3. mysql - 新浪微博中的關注功能是如何設計表結構的?4. angular.js使用$resource服務把數據存入mongodb的問題。5. 如何解決Centos下Docker服務啟動無響應,且輸入docker命令無響應?6. angular.js - 關于$apply()7. MySQL數據庫中文亂碼的原因8. 表單提交驗證,沒反應,求老師指點9. nignx - docker內nginx 80端口被占用10. angular.js - Ionic 集成crosswalk后生成的apk在android4.4.2上安裝失敗???
