Book Image

Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

Book Image

Learning jQuery : Better Interaction Design and Web Development with Simple JavaScript Techniques

Overview of this book

Table of Contents (18 chapters)
Learning jQuery
Credits
About the Authors
About the Reviewers
Preface

The Finished Code


This chapter represents just a small fraction of what can be done on the Web with animated image and text rotators. Taken all together, the code for the headline rotator and image carousel looks like this:

$(document).ready(function() {
  //using each as an 'if' and containing stuff inside a private
  //namespace
  $('#news-feed').each(function() {
    var $this = $(this);
    $this.empty();

    var totalHeight = $this.height();
    var fadeHeight = totalHeight / 4;

    for (var i = 0; i < fadeHeight; i+=2) {
      $('<div></div>').css({
        opacity: i / fadeHeight,
        top: totalHeight - fadeHeight + i
      }).addClass('fade-slice').appendTo(this);
    }
    var $newsLoading = $('<img/>')
      .attr({
        'src': '/cookbook/images/loading.gif',
        'alt': 'loading. please wait'}
      )
      .addClass('news-wait');
     $this.ajaxStart(function() {
       $this.append($newsLoading);
     }).ajaxStop(function() {
       $newsLoading...