gpt4 book ai didi

javascript - 大背景图像导致滚动时滞后

转载 作者:数据小太阳 更新时间:2023-10-29 05:06:15 27 4
gpt4 key购买 nike

我正在构建的网站有 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, Helvetica, Arial, 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, Helvetica, Arial, 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,

对于支持 3d 变换的浏览器,例如:-webkit-transform,您可以尝试以下操作:

your.div { -webkit-transform: translate3d(0,0,1px); }

可能看起来不太像,但执行上述操作会导致相关的 div 进行硬件加速。

如果您遇到任何闪烁问题(已知它们在某些情况下会出现),以下内容应该可以解决您的问题:

your.div { 
-webkit-transform: translate3d(0,0,1px);
-webkit-backface-visibility: hidden;
}

通过大卫沃尔什 - http://davidwalsh.name/translate3d

The use of translate3d pushes CSS animations into hardware acceleration. Even if you're looking to do a basic 2d translation, use translate3d for more power! If your animation is still flickering after switching to the transform above, you can use a few little-known CSS properties to try to fix the problem

希望对您有所帮助。

关于javascript - 大背景图像导致滚动时滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com