简体   繁体   English

在jquery中使用淡入/淡出

[英]Using fade in/fade out with jquery

I am working over on of my student projects and I am new jquery, for the project I have to use jquery to enhance few function and I have learned much to carry out basic tasks, but I am stuck over something very confusing. 我正在研究我的学生项目,我是新的jquery,对于该项目,我必须使用jquery来增强一些功能,并且我学到很多知识来执行基本任务,但是我对一些非常令人困惑的东西感到困惑。

One my scripts actually changes the image of a div container at mouse over function, function is currently fine but make it feel a little beautiful I want to add transition affects to it either through fade in fade out functions or through animate but am unable to work it out with both. 我的一个脚本实际上将鼠标悬停在功能上时更改了div容器的图像,该功能目前还不错,但让它感觉有些漂亮。我想通过淡入淡出功能或通过动画来为其添加过渡效果,但是无法工作两者兼而有之。 I searched over internet but here i am unable to relate those examples to mind here. 我通过互联网搜索,但在这里我无法将这些示例与我联系起来。

I just want to know where can I insert fade in fade out or animate function in this code, to give it a transitioning effect: 我只想知道在此代码中可以在哪里插入淡入淡出或动画功能,以使其具有过渡效果:

$(document).ready(function () {
$(".thumb").hover(function () {
    var dummyImg = $(this).attr("alt");
    $(this).attr("alt", $(this).attr("src"));
    $(this).attr("src", dummyImg);
}, function () {
    var dummyImg = $(this).attr("src");
    $(this).attr("src", $(this).attr("alt"));
    $(this).attr("alt", dummyImg);
});
});

Thank-you! 谢谢!

You want to access the callback function of the fadeIn and fadeOut functions, this will allow you to make changes to the src image and what not. 您要访问fadeIn和fadeOut函数的回调函数,这将使您可以更改src图像,而不能更改。 it would look something like this -> 它看起来像这样->

$(document).ready(function () {
  $(".thumb").hover(function () {
    var dummyImg = $(this).attr("alt");
    $(this).fadeOut('slow', function(){
      //this is now the callback.
      $(this).attr("alt", $(this).attr("src"));
      $(this).attr("src", dummyImg);
      $(this).fadeIn('slow');
    });
  }, function () {
    var dummyImg = $(this).attr("src");
    $(this).fadeOut('slow', function(){
      //this is now the callback.
      $(this).attr("src", $(this).attr("alt"));
      $(this).attr("alt", dummyImg);
      $(this).fadeIn('slow');   
    });
  });
});

Maven, Maven,

Have you thought of using css webkit? 您是否考虑过使用CSS Webkit? This SO article goes into detail for crossfading images - at different rates. 这篇SO文章详细介绍了以不同速率进行淡入淡出图像的方法。 CSS Webkit Transition - Fade out slowly than Fade in CSS Webkit过渡-淡入比淡入慢

You can also make use of a basic event to fade-in/fade-out the image. 您还可以利用基本事件淡入/淡出图像。 This JQuery/JSFiddle SO article makes use of the this reference object: Jquery fadeOut on hover 这篇JQuery / JSFiddle SO文章使用了this参考对象: 悬停时Jquery fadeOut

The basic fade-in / fade-out structure from the JSFiddle.net documention is as follows: JSFiddle.net文档中的基本淡入/淡出结构如下:

$('#show').hover(function() {
    $(this).stop(true).fadeTo("slow", 0);
}, function() {
    $(this).stop(true).fadeTo("slow", 1);
});

~JOL 〜JOL

Personaly, I'd layer the two images (css) so the non-hover version is normally on top. 就个人而言,我会将两个图像(css)分层,因此非悬停版本通常位于顶部。 Then in the hover function, add a $(this).fadeOut('fast') so that the underlying image is displayed. 然后在悬停功能中,添加$(this).fadeOut('fast')以便显示基础图像。

http://jsfiddle.net/Xm2Be/13/ There is an example how you could do that. http://jsfiddle.net/Xm2Be/13/有一个示例,您可以如何做。 Ofcourse, you can set lenght of fade effect by placing some number inside brackets. 当然,您可以通过在括号内放置一些数字来设置淡入淡出效果的长度。 For examle .fadeToggle(5000) will have timing of 5 seconds. 例如,.fadeToggle(5000)的计时时间为5秒。

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

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