Performance optimization:JavaScript Performance Optimization for Mobile Adaptation to Improve Page Responsiveness #308
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Detailed Explanation of the Code
document.addEventListener('DOMContentLoaded', () => {...});
DOMContentLoaded
event. This event is triggered when the HTML document has been completely loaded and parsed. At this point, JavaScript code can safely manipulate DOM elements.function updateMedia() {...}
updateMedia
that decides whether to insert an image or a video based on the current window width. This function is the core part for dynamically adjusting the page content.const mediaContainer = document.querySelector('.media-container');
document.querySelector
to get the first element with the classmedia-container
and stores it in the variablemediaContainer
. This element will be used to insert or update media content.if (window.innerWidth <= 768) {...}
mediaContainer.innerHTML = ...
mediaContainer
element based on the window width check:div
element with abackground-image
style set to the image URL, showing a background image.video
element with attributes (autoplay, muted, loop), and includes the video source (source
element).updateMedia();
updateMedia
function when the page initially loads to ensure the correct media content is inserted based on the current window width.window.addEventListener('resize', updateMedia);
resize
) and calls theupdateMedia
function to dynamically update the media content according to the new window size. This ensures that media content updates when the user resizes the browser window.Summary
This script dynamically inserts media content (image or video) based on the screen size, improving the responsiveness and user experience of the page. It checks and updates media content both on initial page load and when the window size changes, ensuring optimal display on different devices.
Dynamic Element Insertion with JavaScript
By using JavaScript to determine screen width and decide whether to insert a video or an image, you can completely avoid loading video files on mobile devices and load appropriate images instead.
Benefits
Additional Optimizations
Using these methods, you can effectively handle media adaptation for mobile and PC platforms, improving page responsiveness and user experience.