Bài viết hướng dẫn JavaScript này chỉ bạn cách sử dụng CSS, JavaScript, AJAX cũng như kết hợp chúng để tải về trước các hình ảnh trên trang web một cách thông minh hơn, nhằm rút ngắn thời gian chờ đợi đối với khách ghé thăm. Các chỉ dẫn khá chi tiết, đầy đủ cũng như dễ dàng làm theo, vui lòng vào trang chi tiết để xem thêm.
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Tạo video bằng AI chỉ với giọng nói hoặc văn bản
Ứng dụng video AI MIỄN PHÍ hàng đầu của bạn! Tự động hóa video AI đầu tiên của bạn. Tạo Video Chuyên Nghiệp Của Bạn Trong 5 Phút Bằng AI Không Cần Thiết Bị Hoặc Kỹ Năng Chỉnh Sửa Video. Sản xuất video dễ dàng dành cho nhà tiếp thị nội dung.
Method 2: Preloading with JavaScript Only
As effective as the previous method happens to be, I generally find it to be too tedious and time-consuming to actually implement. Instead, I generally prefer to preload images using a straight-up slice of JavaScript. Here are a couple of JavaScript-only preloading methods that work beautifully in virtually every modern browser..
JavaScript Method #1
Unobtrusive, gracefully degrading, and easy to implement, simply edit/add the image paths/names as needed - no other editing required:
This method is especially convenient for preloading large numbers of images. On one of my gallery sites, I use this technique to preload almost 50 images. By including this script on the login page as well as internal money pages, most of the gallery images are preloaded by the time the user enters their login credentials. Nice.
JavaScript Method #2
Here's another similar method that uses unobtrusive JavaScript to preload any number of images. Simply include the following script into any of your web pages and edit according to the proceeding instructions:
As you can see, each preloaded image requires a variable definition, "img1
=
new
Image();
", as well as a source declaration, "img3.src
=
"../path/to/image-003.gif";
".
By replicating the pattern, you can preload as many images as
necessary. Hopefully this is clear - if not, please leave a comment and
someone will try to help you out.
We can even improve this method a bit by delaying preloading until after the page loads. To do this, we simply wrap the script in a function and use addLoadEvent()
to make it work:
Ahhh, the joys of JavaScript!
Method 3: Preloading with Ajax
As if all of that weren't cool enough, here is a way to preload images using Of Geeks and letters, and uses the DOM to preload not only images, but CSS, JavaScript, and just about anything else. The main benefit of using Ajax over straight JavaScript is that JavaScript and CSS files can be preloaded without their contents affecting the current page. For images this is not really an issue, but the method is clean and effective nonetheless.
As is, this code will preload three files: "preload.js
", "preload.css
", and "preload.png
". A timeout of 1000ms
is also set to prevent the script from hanging and causing issues with normal page functionality.
To wrap things up, let's look at how this preloading session would look written in plain JavaScript:
Here we are preloading our three files upon page load by creating three elements via the DOM. As mentioned in the original article, this method is inferior to the Ajax method in cases where the preloaded file contents should not be applied to the loading page.
Know some triks?
I love these preloading tricks so much, I could just squeeze something. If you know of any good preloading tricks, including any improvements to the techniques shared here, kick start my heart with your wise words of preloading wisdom ;)
- Lượt gửi (0)
- Mới