Home Web Front-end JS Tutorial jQeury淡入淡出需要注意的问题_jquery

jQeury淡入淡出需要注意的问题_jquery

May 16, 2016 pm 06:19 PM
fade in and fade out

前两天看到橡树小屋朋友发表的《JQuery 实现图片轮播效果》,比较有趣,发现他是使用fadeIn和fadeOut实现图片淡入淡出轮换的。当时曾担心他的例子中如果连续多次点击,所产生的动画会不会有延时。但我连续点击了几下,没看到明显的延时,就没再多想。
  众所周知,jQuery所产生的动画效果默认会进入列队的。假如:点击一下,产生动画3秒钟。然后我快速的连点3次。那么要等到9秒钟,所有动画才能结束。animate是自定义动画,可以很容易的设定动画是否进入列队。但使用fadeIn和fadeOut就麻烦了。
  看到有的Flash网站的图标,鼠标一放上去图标就缓缓变了,移开又会缓缓变回来,很是漂亮。我打算用jQuery也做做看,能不能做出类似的效果。因为自己练手,就随便拉两张图片:

复制代码 代码如下:


jQeury淡入淡出需要注意的问题_jquery
jQeury淡入淡出需要注意的问题_jquery


这样第一张就会覆盖第二张图片,那我只要淡入淡出第一张图片就能实现特效了。于是就使用了hover,fadeIn和fadeOut,简单的实现了
复制代码 代码如下:

$(document).ready(function () {
$("div").hover(
function () { $("#1").fadeOut(1000); },
function () { $("#1").fadeIn(1000); }
);
});

但这样问题出来了,如果我在图片上不停地快速的移入移出鼠标。那么动画都进入列队了,那么动画就会一直在动,很是不好看。

于是我打算使用:dequeue(),定义:Removes a queued function from the front of the queue and executes it.
我想如果不停的移入移出,那么就会删除上一个操作在列队中的动画。这样就会执行最后的动画了。
复制代码 代码如下:

function () { $("#1").dequeue().fadeOut(1000); },
function () { $("#1").dequeue().fadeIn(1000); }

可是更麻烦的情况出现了,当不停地移入移出鼠标时,有时图片都没了,有时不变了。怎么回事?

然后又想到使用:stop(),定义:
Stops all the currently running animations on all the specified elements.
If any animations are queued to run, then they will begin immediately.
复制代码 代码如下:

function () { $("#1").stop().fadeOut(1000); },
function () { $("#1").stop().fadeIn(1000); }

我停止前面所有的列队,总算可以了吧!但是却出现了图片淡到一半,不动了!就像两个图片叠加显示了一样。
又是怎么回事?
  直到我在stop中加参数,图片才能正常显示。
  clearqueue (可选)boolean
  如果设置成true,则清空队列。可以立即结束动画。
  gotoend (可选)boolean
  让当前正在执行的动画立即完成,并且重设show和hide的原始样式,调用回调函数等。
复制代码 代码如下:

function () { $("#1").stop(true,true).fadeOut(1000); },
function () { $("#1").stop(true, true).fadeIn(1000); }

但这样就会出现执行完毕,突然显示整图的情况,就没有了淡入淡出的那样的效果了。
  没办法,只有使用animate了。
复制代码 代码如下:

function () { $("#1").stop().animate({ 'opacity': 0 }, 1000); },
function () { $("#1").stop().animate({'opacity':1}, 1000); }
或:
function () { $("#1").animate({ 'opacity': 0 }, { queue: false, duration: 1000 }); },
function () { $("#1").animate({ 'opacity': 1 }, { queue: false, duration: 1000 }); }

这才实现了想要的完美效果。
  
  总结一下,使用stop和dequeue理论都是可以的,但为什么却出错?我也不太清楚,估计是jQuery库的问题吧,
这个要查原文件才找得到问题。但以后使用fadeIn和fadeOut真的注意一下。当然,到橡树小屋朋友的
JQuery实现图片轮播效果》肯定是个好例子,直到我把时间改到2000才看出来有延迟的。只有我故意找毛病的人才会
这么干,其他哪还有人会设这么长的时间的。有兴趣的朋友可以去橡树小屋那学习一下,既简单又漂亮实用的例子。
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles