8 Energizing pages with animations and effects

This chapter covers

  • Showing and hiding elements without animation
  • Showing and hiding elements using core animation
  • Extending the core easing functions
  • Writing custom aniamtions
  • Controlling animation and function queuing

8.1 Showing and hiding elements


$('div').hide().addClass('fun');

8.1.1 Implementing a collapsible "module"


$('.icon-roll').click(function () {
  var $body = $(this).closest('.module').find('.body');
  if ($body.is(':hidden')) {
    $body.show();
  } else {
    $body.hide();
  }
});

8.1.2 Toggling the display state of elements


$('.icon-roll').click(function() {
  $(this).closest('.module').find('.body').toggle();
});

8.2 Animating the display state of elements


  • Show and hide
  • Fade in and fade out
  • Slide down and slide up

8.2.1 Showing and hiding elements gradually


hide(duration[, easing][, callback])
hide(options)
hide()
Parameters
duration (Number|String)
easing (String)
callback (Function)
options (Object)
Returns
The jQuery collection.


toggle(duration[, easing][, callback])
toggle(options)
toggle(condition)
toggle()
Parameters
condition (Boolean)
Returns
The jQuery collection.

$('.icon-roll').click(function() {
  var $icon = $(this);
  $icon.closest('.module').find('.body').toggle('slow', function() {
    $icon.text($(this).is(':hidden') ? '+' : '-');  
  });
});

8.2.2 Introducing the jQuery Effects Lab Page

8.2.3 Fading elements into and out of existence


fadeIn(duration[, easing][, callback])
fadeIn(options)
fadeIn()
Returns
The jQuery collection.


fadeOut(duration[, easing][, callback])
fadeOut(options)
fadeOut()
Returns
The jQuery collection.


fadeToggle(duration[, easing][, callback])
fadeToggle(options)
fadeToggle()
Returns
The jQuery collection


fadeTo(duration, opacity[, easing][, callback])
Parameters
opacity (Number)
Returns
The jQuery collection.


8.2.4 Sliding elements up and down


slideDown(duration[, easing][, callback])
slideDown(options)
slideDown()
Returns
The jQuery collection.


slideUp(duration[, easing][, callback])
slideUp(options)
slideUp()
Returns
The jQuery collection.


slideToggle(duration[, easing][, callback])
slideToggle(options)
slideToggle()
Returns
The jQuery collection.


8.2.5 Stopping animations


stop([queue][, clearQueue[, goToEnd]])
Parameters
queue (String)
clearQueue (Boolean)
goToEnd (Boolean)
Returns
The jQuery collection


finish([queue])
Parameters
queue (String)
Returns
The jQuery collection.


8.3 Adding more easing functions to jQuery

8.4 Creating custom animations


animate(properties[[, duration][, easing]][, callback])
animate(properties[, options])
Parameters
properties (Object)
duration (Number|String)
easing (String)
callback (Function)
options (Object)
Returns
The jQuery collection.


8.4.1 A custom scale animation


$('.animated-elements').each(function() {
  var $this = $(this);
  $this.animate({
    width: $this.width() * 2
    height: $this.height() * 2
    },
    2000
  );
});

8.4.2 A custom drop animation


$('.aniamted-elements').each(function(){
  var $this = $(this);
  $this.css('postion', 'relative').animate({
    opacity: 0,
    top: $(window).height() - $this.height() - $this.position().top
  },
  'normal',
  function() {
    $this.hide();
  }
  );
});

8.4.3 A custom puff animation


$('.animated-elements').each(function() {
  var $this = $(this);
  var position = $this.position();
  $this.css({
    position: 'absolute',
    top: position.top,
    left: postion.left
  }).animate({
    opacity: 'hide',
    width: $this.width() * 5,
    height: $this.height() * 5,
    top: position.top - ($this.height() * 5 / 2),
    left: position.left - ($this.width() * 5 / 2)
  },
  'fast'
  );
});

8.5 Animations and queuing

8.5.1 Simultaneous animations


$('#test-subject').animate({left: '+=256'}, 'slow');
$('#test-subject').aniamte({top: '+=256'}, 'slow');
console.log(1);
$('#test-subject').animate({left: '+=256'}, 'slow');
console.log(2);

"1" and "2" are printed immediately on the console


8.5.2 Queuing functions for exection


var $moonImage = $('img[alt="moon"]');
$moonImage.animate({left: '+=256'}, 2500);
$moonImage.animate({top: '+=256'}, 2500);
$moonImage.animate({left: '-=256'}, 2500);
$moonImage.animate({top: '-=256'}, 2500);

var $moonImage = $('img[alt="moon"]');
$moonImage.animate({left: '+=256'}, 2500, function(){
 $moonImage.animate({top: '+=256'}, 2500, function(){
 $moonImage.animate({left: '-=256'}, 2500, function(){
 $moonImage.animate({top: '-=256'}, 2500);
 });
 });
});

callback complicated


queue([name])
queue([name], function)
queue([name], queue)
Parameters
name (String)
function (Function)
queue (Array)
Returns
An array of functions for the first form. The jQuery collection for the remaining forms.


dequeue([name])
Parameters
name (String)
Returns
The jQuery collection.

var $images = $('img');
$images
 .queue('chain', function(next) {
 console.log('First: ' + $(this).attr('alt'));
 next();
 })
 .queue('chain', function(next) {
 console.log('Second: ' + $(this).attr('alt'));
 next();
 })
 .queue('chain', function() {
 console.log('Third: ' + $(this).attr('alt'));
 $(this).dequeue('chain');
 })
 .queue('chain', function() {
 console.log('Fourth: ' + $(this).attr('alt'));
 });

$('button').click(function() {
 $images.dequeue('chain');
 });

clearQueue([name])
Parameters
name (String)
Returns
The jQuery collection.


***delay(duration[, queueName])
Parameters
duration (Number|String)
queueName (String)

$('#my-image')
 .slideUp('slow')
 .delay(1000)
 .slideDown('fast');

8.5.3 Inserting functions into the effects queue


var $moonImage = $('img[alt="moon"]');
$moonImage.animate({left: '+=256'}, 2500);
$moonImage.animate({top: '+=256'}, 2500);
$moonImage.queue('fx',
 function() {
 $(this)
 .css({backgroundColor: 'black'});
 .dequeue('fx');
 }
);
$moonImage.animate({left: '-=256'}, 2500);
$moonImage.animate({top: '-=256'}, 2500);
$moonImage.animate({top: '-=256'}, 2500, function() {
 $(this).css({backgroundColor: 'white'});
});

8.6 Summary


animate()
show() and hide()
toggle()
fadeOut() and fadeIn()
fadeToggle()
fadeTo()
slideUp() slideDown()
slideToggle()
queue animations

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,491评论 2 45
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,881评论 18 139
  • 沙盘 一顿晚餐下来,任予食不知味。最终,她趁着宋司楚起身去结账的时候,翻出手机,点开了苏沐发给自己的那一则信息,但...
    拾月唯一阅读 632评论 0 1
  • 1、说明:创建数据库CREATE DATABASE database-name2、说明:删除数据库drop dat...
    codeSirCao阅读 455评论 0 2
  • 相信熟悉这本书的人看到文章标题就知道我要分享的书了—《小狗钱钱》,没错,就是这本书故事书。 钱钱是一只特别的狗,它...
    劉淑嫻阅读 333评论 3 1