我正在构建的网站有4个大的背景图像,占用了用户浏览器的整个高度和宽度.它们被实现为CSS背景div.问题是,当在较大的屏幕尺寸上滚动时,它非常滞后且不连贯.当用户按下按钮时,这些图像之间的滚动是通过
JavaScript自动完成的,所以这是我网站核心功能的一部分,我必须找到一种方法来防止延迟.
到目前为止,我已经尝试通过JS预加载图像并将图像从PNG转换为JPEG(增加压缩并降低质量)服务器端.这些都没有奏效.
图像的最小高度可以是630像素.如何在部分之间滚动时防止延迟?
这是我的代码:
CSS:
body { height: 100%; margin: 0px; font-family: HelveticaNeue,Helvetica,Arial,sans-serif; } .area { height: 630px; border: 0px solid red; background: repeat-x; margin-bottom: 0px; } a { text-decoration: none; } h1,h2,h3,h4,h5,h6 { font-family: Av,sans-serif; color: #292E37; font-weight: lighter; } #top { position: fixed; width: 100%; height: 10%; background: #292E37; Box-shadow: inset 0px -1px 5px #000; z-index: 1000; } #navigation { float: right; height: 100%; } #bottom { width: 100%; position: fixed; bottom: 0px; padding: 10px; background: #292E37; Box-shadow: inset 0px 1px 5px #000; text-shadow: 0px 1px 0px #000; color: #fff; } #sceneSelection { top: 20%; position: fixed; padding: 10px; } #info { margin-top: 50px; margin-bottom: 50px; } .Box { margin-top: 50px; padding: 75px; background: #292E37; Box-shadow: inset 0px 1px 5px #000; text-shadow: 0px 1px 0px #000; color: #fff; } .nav { position: relative; top: 38%; height: 100%; margin-right: 35px; display: inline-block; color: #fff; text-shadow: 0px 1px #000; } .nav:hover { color: #EA5555; } .nimage { float: left; width: 16px; height: 16px; position: relative; top: 5%; left: -20%; } .home { background: url(site_images/icons/nav/home.png); } .pricing { background: url(site_images/icons/nav/pricing.png); } .features { background: url(site_images/icons/nav/features.png); } .blog { background: url(site_images/icons/nav/blog.png); } .contact { background: url(site_images/icons/nav/contact.png); } .about { background: url(site_images/icons/nav/us.png); } .logo { font-size: 2em; text-shadow: 0px 1px #000; padding-top: 10px; padding-left: 15px; color: #EA5555; font-family: Av,sans-serif; } .red { color: #EA5555; } .white { color: #fff; text-shadow: 0px 1px 0px #000; font-weight: bold; } .dark { color: #202020; } .center { text-align: center; } .left { text-align: left; } .right { text-align: right; } .larger { font-size: 1.25em; } .buttoni { -webkit-border-radius: 2px; -moz-border-radius: 0px; border-radius: 4px; background: #ddd; display: block; color: #ccc; font-size: 14pt; height: 50px; text-align: right; margin: 10px; cursor: pointer; color: #505050; } .buttoni:hover { background: #EA5555; color: #fff; } .btext { padding: 15px; position: relative; top: 25%; } .groundi { background: url(ground_button.png); } .skyi { background: url(sky_button.png); } .stratospherei { background: url(stratosphere_button.png); } .spacei { background: url(space_button.png); } .image { height: 50px; width: 50px; float: left; border-top-left-radius: 5px; border-bottom-left-radius: 5px; } li { color: #EA5555; } li span { color: #505050; }
HTML:
<div class="space area" id="a4"> </div> <div class="stratosphere area" id="a3"> </div> <div class="sky area" id="a2"> </div> <div class="ground area" id="a1"> </div>
JavaScript的:
function scroll_to(id,speed,margin) { $('html,body').animate({ scrollTop: $('#' + id).offset().top - margin },speed); } function match_height() { var heights = [11,630,693,756,819,882,945,1008,1071,1134,1197,1260,1323,1386,1449,1512,1575,1638,1701,1764,1827,1890,1953,2016,2079,2142,2205,2268,2331,2394,2457,2520]; var browsery = $(window).height(); var i = 0; while(browsery > heights[i]) { i++; } var h = heights[i]; $(".area").css("height",h + "px"); $(".area").css("width","100%"); $(".ground").css("background","url(scenes/ground/" + h + ".png)"); $(".sky").css("background","url(scenes/sky/" + h + ".png)"); $(".stratosphere").css("background","url(scenes/stratosphere/" + h + ".png)"); $(".space").css("background","url(scenes/space/" + h + ".png)"); } match_height(); var pos = 0; $(".buttoni").click(function() { var id = $(this).attr("id"); if(pos != id) { scroll_to("a" + id,2000,0); } pos = id; });
解决方法
根据我的理解,OP中起草的问题和解决方案有两个方面:
>最初,在match_height()函数中,OP作者检索最适合屏幕高度的图像,以便在完成动画时用户看到一个完整的背景图像.
>初始加载后,用户可以借助一些触发scroll_to()函数及其包含动画的按钮来上下移动各个部分(及其各自的背景图像).这是实际问题所在的位置.
我的努力和由此产生的小提琴专注于scroll_to()函数和相关的动画.我采用了以下措施,结合起来(根据我的主观观察)“更顺畅”的哄骗经历:
>原始动画发生在’html’和’body’上,我只是将jQuery选择器缩减为一个选择器.为了能够使用jQuery 1.9(不推荐使用jQuery.browser),我正在使用特征检测来获取“正确”的选择器:
function getScrollerSelector() { var $body = $("<body/>"); $body.scrollTop(1); return $body.scrollTop() == 1 ? "body" : "html"; }
>为了减少浏览器的处理负载,我正在应用一个逻辑,根据CSS,在滚动期间将不可见部分的背景图像设置为none:
.scrolldown.scrollto-a2 #a1,.scrolldown.scrollto-a3 #a1,.scrolldown.scrollto-a3 #a2,.scrolldown.scrollfrom-a3 #a4,.scrolldown.scrollfrom-a2 #a4,.scrolldown.scrollfrom-a2 #a3,.scrollup.scrollto-a3 #a4,.scrollup.scrollto-a2 #a4,.scrollup.scrollto-a2 #a3,.scrollup.scrollfrom-a2 #a1,.scrollup.scrollfrom-a3 #a1,.scrollup.scrollfrom-a3 #a2 { background: none; }
>我玩线性缓和,但这并不一定能改善任何东西
总而言之,滚动对我来说似乎不再起伏不定,但请注意这也取决于客户端计算机的处理能力.
这是scroll_to()函数:
function scroll_to(id,margin) { var currentScrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; var scrollTop = $('#' + id).offset().top - margin; var direction = scrollTop > currentScrollTop ? "down" : "up"; $("body").addClass("scroll"+direction + " scrollto-"+id + " scrollfrom-"+getScrollFrom(direction)); $( scrollerSelector ).animate({ scrollTop: scrollTop },{ //easing: 'linear',duration: speed,complete: function() { $("body").removeClass("scrollup scrolldown scrollto-a1 scrollto-a2 scrollto-a3 scrollto-a4 scrollfrom-a1 scrollfrom-a2 scrollfrom-a3 scrollfrom-a4"); } } ); }