JavaScript 动态加载页面 js文件
JavaScript 代码:
$(function(){ $("body").scrollTop(0); var nt = !1; var h=window.innerHeight; function scroll(){ $("#maindatajs_load [data-func]:not(isLoad)").each(function() { var str = $("body").scrollTop()+h; var htr=$(this).offset().top; var df = $(this).attr("data-func"); var isLoad = $(this).attr("isLoad"); if(!isLoad && htr<=str && df!="" && df!= "undefined"){ console.log(df) setTimeout(function(){ platformfun[df](); },500); $(this).attr("isLoad", true); } }); } scroll(); $(window).on("scroll", scroll); });
HTML 代码:
<div id="maindatajs_load">
<div class="div-dom" data-func="code1"></div>
<div class="div-dom" data-func="code2"></div>
<div class="div-dom" data-func="code3"></div>
<div class="div-dom" data-func="code4"></div>
<div class="div-dom" data-func="code5"></div>
<div class="div-dom" data-func="code6"></div>
</div>
data-func的值为需要加载的js文件名称或者js函数方法,当滚动页面时,判断 data-func属性的div是否显示在屏幕中,然后加载其对应的js文件