search
HomeWeb Front-endJS TutorialjQeury淡入淡出需要注意的问题_jquery

前两天看到橡树小屋朋友发表的《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
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
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Example Colors JSON FileExample Colors JSON FileMar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 jQuery Syntax Highlighters10 jQuery Syntax HighlightersMar 02, 2025 am 12:32 AM

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

8 Stunning jQuery Page Layout Plugins8 Stunning jQuery Page Layout PluginsMar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

10  JavaScript & jQuery MVC Tutorials10 JavaScript & jQuery MVC TutorialsMar 02, 2025 am 01:16 AM

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

What is 'this' in JavaScript?What is 'this' in JavaScript?Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.