Home > Web Front-end > JS Tutorial > Ideas for switching pictures when the mouse slides over them in JS_javascript techniques

Ideas for switching pictures when the mouse slides over them in JS_javascript techniques

WBOY
Release: 2016-05-16 17:22:40
Original
1771 people have browsed it

On many websites, we will find that when the mouse slides over a picture, the picture switches to another picture. Here the editor talks about how this is achieved.

Before writing Javascript code we must have experimental HTML code

Copy the code The code is as follows:




Jquery deal images








Let’s focus on analyzing the JS code
Copy the code The code is as follows:

$(document).ready(function(){
var newImage = new Image(); //Preload image
var oldImage = $('img').attr('src' );
newImage.src = './images/img03.jpg';
$('img').hover(function(){ //Mouse over the image to switch
$('img' ).attr('src',newImage.src);
},
function(){
$('img').attr('src',oldImage);
});
});

What everyone is confused about here is why do we need to preload images? Because unlike our local debugging on the website, the images download so quickly. If it is on the Internet, when the mouse slides over the image to be switched, the replacement image will be temporarily downloaded, and the process of loading the image is relatively slow. So we can solve this problem by preloading images.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template