简体   繁体   English

通过隐藏或删除元素来优化移动Web应用程序?

[英]Optimize mobile web app by hiding or removing elements?

I'm creating a mobile version of a web application I have recently made. 我正在创建我最近制作的Web应用程序的移动版本。 I am trying to optimize the application to make it as fast as possible. 我正在尝试优化应用程序以使其尽可能快。 There are some elements inside the application that should not be visible for the mobile version. 应用程序中有一些元素对于移动版本不可见。

How should I go about dealing with these elements? 我应该如何处理这些元素?

Do I use JavaScript to completely remove the element from the DOM? 我是否使用JavaScript从DOM中完全删除元素? Do I use a CSS media query and simply change the visibility of the element to hidden? 我是否使用CSS媒体查询并将元素的可见性更改为隐藏? Do I use JavaScript and change the visibility of the element to hidden? 我是否使用JavaScript并将元素的可见性更改为隐藏?

I'm looking for the most efficient way as my objective is to improve performance (even if it's only a very small difference). 我正在寻找最有效的方法,因为我的目标是提高性能(即使只有很小的差别)。

As u said it should optimized for the performance. 正如您所说,应该针对性能进行优化。 One thing to keep in mind is using images of right size. 要记住的一件事是使用适当大小的图像。 for this you can use css background-image to some elements and use css media query to select the right size background-image. 为此,您可以对某些元素使用css背景图像,并使用css媒体查询来选择合适大小的背景图像。

Eg: 例如:

#banner{
background-image: big-image.jpg;
}

@media all and (max-width: 640px){
#banner{
background-image: small-image.jpg;
}
}

@media all and (max-width: 420px){
#banner{
background-image: tiny-image.jpg;
}
}

for rest of the elements you can just remove() if really bulky html or hide if its very few lines. 对于其余元素,您可以只删除remove()如果html确实很笨重,或者隐藏起来,如果它的行数很少。

To avoid loading desktop images on mobile devices there is a nice trick to lazy load them by adding data attributes to the image tags instead of src. 为了避免在移动设备上加载桌面图像,有一个不错的技巧,可以通过将数据属性(而不是src)添加到图像标签中来延迟加载它们。

 <img data-lazy-load-event="immediately" data-lazy-load-for-media="desktop" data-lazy-load-src="image.jpg" />

In JavaScript you can search your DOM for all images with the data-lazy-load-src and then load them according to their media type, by adding the data-lazy-load-src attribute to their src attribute. 在JavaScript中,您可以在DOM中搜索所有带有data-lazy-load-src的图像,然后通过将data-lazy-load-src属性添加到src属性来根据其媒体类型加载它们。

For elements that you can't avoid loading, use JavaScript to remove them from the DOM. 对于无法避免加载的元素,请使用JavaScript将其从DOM中删除。

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

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