jquery achieves image gradient effect

WBOY
Release: 2023-05-14 13:22:39
Original
538 people have browsed it

In web design, the application of image gradient effect is becoming more and more common. It can not only increase the beauty of the web page, but also attract the user's attention. Today, we will implement the image gradient effect through jQuery.

First, in HTML, we need to insert a picture first:

<img src="image.jpg" alt="这是一张图片" id="image">
Copy after login

Then, in JavaScript, we need to store the src attribute of the picture into a variable:

var image = $("#image");
var src = image.attr("src");
Copy after login

Then, we set the src attribute of the image to an empty string so that the image appears blank:

image.attr("src", "");
Copy after login

Next, use jQuery to preload the image:

$("<img>").attr("src", src).load(function() {
  // 图片加载完成后执行以下代码
  image.fadeOut(function(){
    $(this).attr("src", src).fadeIn();
  });
});
Copy after login

In the above In the code, we create a new img tag and set the image address to the previously saved src variable. After the new image is loaded, we perform a fade out effect on the original image, then set the src attribute of the new image to the previously saved image address, and use the fadeIn effect to make the new image appear slowly.

Finally, we need to put the entire code into $(document).ready() to ensure that the code can be executed after the document is loaded:

$(document).ready(function() {
  var image = $("#image");
  var src = image.attr("src");
  image.attr("src", "");
  $("").attr("src", src).load(function() {
    image.fadeOut(function(){
      $(this).attr("src", src).fadeIn();
    });
  });
});
Copy after login

Through the above code, we successfully Use jQuery to achieve the image gradient effect, making the web page more beautiful and attracting the user's attention.

The above is the detailed content of jquery achieves image gradient effect. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!