jQuery Effect: Use dequeue to end a custom queue function which allows the queue to keep going
jQuery Effect : Exercise-4 with Solution
Use dequeue to end a custom queue function which allows the queue to keep going.
Solution:
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>Use dequeue to end a custom queue function which allows the queue to keep going</title>
</head>
<body>
<button>Start</button>
<div </div>
</body>
</html>
CSS Code:
div {
margin: 3px;
width: 50px;
position: absolute;
height: 50px;
left: 10px;
top: 30px;
background-color: #9F81F7;
}
div.new {
background-color: #3B0B24;
}
JavaScript Code:
$( "button" ).click(function() {
$( "div" )
.animate({ left:"+=250px" }, 2000 )
.queue(function() {
$( this ).toggleClass( "new" ).dequeue();
})
.animate({ left:"-=250px" }, 2000 );
});
Note : In jQuery dequeue( [queueName ] ) method is used to execute the next function on the queue for the matched elements. It has the following parameter :
- queueName : A string containing the name of the queue.[Type: String]
Live Demo:
See the Pen jquery-effect-exercise-4 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Set a timer to delay execution of subsequent items in the queue.
Next: Fade in and fade out all division elements.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics