简体   繁体   English

为什么我会有这么多滞后?

[英]Why do I have so much lag?

I have been working on this new website but the "buttons" I am using are causing a ton of lag, I would like to remove the lag if possible. 我一直在这个新网站上工作,但是我使用的“按钮”造成了大量的滞后,如果可能的话,我想消除滞后。

The website is here: http://lano-project.org/ 网站在这里: http : //lano-project.org/

The troubled code is here: 问题代码在这里:

<td>
    <a href="templink-info.html">
    <img style="display: none" src="images/icons/hover-info.png"/>
    <img src="images/icons/info.png"
        onmouseover="this.src='images/icons/hover-info.png';
            document.getElementById('home_text').style.display = 'none';
            document.getElementById('info_text').style.display = 'block';"
        onmouseout="this.src='images/icons/info.png';
            document.getElementById('home_text').style.display = 'block';
            document.getElementById('info_text').style.display = 'none';"
        onclick=""/>
    </a>
    <h3>Info</h3>
</td>

with relevant css: 与相关的CSS:

#icon tr td img{
    width: 100px;
    height: 100px;}
#icon tr td p{
    margin: 0px;}
#icon tr td{
    text-align: center;
    width: 150px;}
#icon{
    margin-left: auto;
    margin-right: auto;}

https://css-tricks.com/snippets/css/basic-link-rollover-as-css-sprite/ https://css-tricks.com/snippets/css/basic-link-rollover-as-css-sprite/

You can boost your responsiveness by creating only one image with both states of your button that is twice as large as the button itself. 您可以通过仅创建一个按钮状态为按钮本身两倍的图像来提高响应速度。 Then, on mouseover, just change the background-position property using css instead of loading a new image every time. 然后,在鼠标悬停时,只需使用CSS更改background-position属性,而不是每次都加载新图像。 This effectively "slides" the image so that the correct part of it shows "through" the button. 这样可以有效地“滑动”图像,以便图像的正确部分“通过”按钮显示。 This operation is very fast, and I think you'll see a big difference. 此操作非常快,我想您会发现很大的不同。

Compress your images and it the website will load a bit faster. 压缩图像,网站加载速度会更快。

Post it on jsFiddle and specify the problem, because for now, I don't understand what do you want, because your website loads normally without any lag. 将其发布在jsFiddle上并指定问题,因为到目前为止,我不知道您想要什么,因为您的网站正常加载没有任何滞后。

If you want to create an attribute with image, you can change the background of attribute or just use JS for.. some cool stuff you're trying to do. 如果要使用图像创建属性,则可以更改属性的背景,或仅将JS用于...。

EDIT: I've done in the past, my solution is to use background-image and change it on :hover 编辑:我过去做过,我的解决方案是使用背景图像并在:hover上更改它

http://puu.sh/qc98m/a828b9ae4e.png like that. 像这样的http://puu.sh/qc98m/a828b9ae4e.png

The mouseover is only slow when hovering the images for the first time. 仅当第一次将图像悬停时,鼠标悬停才缓慢。 What's happening behind the scenes, is that the new (hover) images weren't loaded into the browser's cache when the page loaded, have to be loaded on first mouseover, and hence cause the (visual) delay. 幕后发生的事情是,当页面加载时,新的(悬停)图像没有被加载到浏览器的缓存中,必须在第一次鼠标悬停时加载,从而导致(视觉)延迟。 Subsequent cursor passes are as fast as would normally be expected. 随后的光标通过与通常预期的一样快。

One possible solution would be to preload images (which would obviously happen in the background) immediately when the page loads. 一种可能的解决方案是在页面加载后立即预加载图像(显然会在后台发生)。 From a similar question : 类似的问题

function preloadImage(url)
{
    var img=new Image();
    img.src=url;
}

preloadImage('images/icons/hover-info.png');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM