Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将jQuery fadeIn和fadeOut添加到鼠标事件函数中?_Jquery_Fadein_Fadeout_Mouseenter_Mouseleave - Fatal编程技术网

如何将jQuery fadeIn和fadeOut添加到鼠标事件函数中?

如何将jQuery fadeIn和fadeOut添加到鼠标事件函数中?,jquery,fadein,fadeout,mouseenter,mouseleave,Jquery,Fadein,Fadeout,Mouseenter,Mouseleave,我正在使用jQuery更改鼠标事件(悬停)上的图像,但我无法找到为以下函数添加fadeIn和fadeOut的最佳方法 var sourceIn = function () { var $this = $(this); var newSource = $this.data('alt-src'); $this.data('alt-src', $this.attr('src')); $thi

我正在使用jQuery更改鼠标事件(悬停)上的图像,但我无法找到为以下函数添加
fadeIn
fadeOut
的最佳方法

        var sourceIn = function () {
            var $this = $(this);
            var newSource = $this.data('alt-src');
            $this.data('alt-src', $this.attr('src'));
            $this.attr('src', newSource);
        }
        var sourceOut = function () {
            var $this = $(this);
            var newSource = $this.data('alt-src');
            $this.data('alt-src', $this.attr('src'));
            $this.attr('src', newSource);
        }

        $(function () {
            $('img.toggle').mouseenter(sourceIn);
            $('img.toggle').mouseleave(sourceOut);
        });
我已经尝试了很多方法来添加
fadeToggle
,但我不确定最好的技术是什么


jsidle示例:

而不仅仅是更改
src
,使用
fadeOut
fadeIn

$this.fadeOut(function() {
    $this.attr('src', newSource).fadeIn();
});
演示: