Results 1 to 3 of 3

Thread: understanding crossfading effect

  1. #1
    Join Date
    Feb 2011

    understanding crossfading effect

    HI there, I wonder if anybody could help me to understand correctly the jquery code for crossfading effects, quite a neat effect actually http://www.simonbattersby.com/blog/s...age-crossfade/
    I had some trouble understanding the css but I was kindly helped http://webdeveloper.com/forum/showth...=1#post1192483 so that's sorted.
    Back to the javascript.
    Couple of things in this code that are a bit new and somewhat odd to me:
    function cycleImages(){
          var $active = $('#portfolio_cycler .active');
          var $next = ($('#portfolio_cycler .active').next().length > 0) ? $('#portfolio_cycler .active').next() : $('#portfolio_cycler img:first');
          $next.css('z-index',2);//move the next image up the pile
    	  $active.fadeOut(1500,function(){//fade out the top image
    	  $active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
          $next.css('z-index',3).addClass('active');//make the next image the top one
    // run every 7s
    setInterval('cycleImages()', 7000);
    I need to say I am at the beginning of my jquery learning sessions, so sorry if I ask obvious questions.
    Take this line:
    var $active = $('#portfolio_cycler .active');
    1)First and foremost, why there is a $ before the name of the variable? I thought that a variable is declared and assigned a value in this fashion
    var var_name = ...
    . Is
    var $var_name = ...
    the same?
    2)Then still the first line, this bit
    $('#portfolio_cycler .active')
    I haven't come across this syntax before. Is this selector selecting a div with an id of portfolio_cycler but what's that class active doing there?! Does it mean that we are selcting only portfolio_cycler with a class of active? SOrry I am a bit confused by that.
    Moving on this line:
    var $next = ($('#portfolio_cycler .active').next().length > 0) ? $('#portfolio_cycler .active').next() : $('#portfolio_cycler img:first');
    . IS this to say that if the next element in the portfolio_cycler is bigger than zero - meaning it is not the first image - then the variable next is equal to the next image otherwise it is the first image

    Next one:
    WHy are we using the method show() in here why do we want to make the precedent image visible is it has been replace by the following one?

    Hope somebody can give me a hand, thanks

  2. #2
    Join Date
    Dec 2005
    So first, off, in case you weren't aware, this JavaScript code uses the jQuery library which allows you to retrieve and manipulate DOM elements using a much more palatable syntax than native JavaScript code. Check out the docs on http://api.jquery.com/ to find out all about the DOM manipulation methods that jQuery exposes.

    As for your questions:

    1) $ is just a character like X or _ so it can start a JavaScript variable name. Some developers use $ in front of JavaScript variables that are supposed to contain jQuery objects

    2) As you'll read in jQuery's API $('#portfolio_cycler .active') gets any element with a class name of active that is a descendant of an element with an id of portfolio_cycler.

    In my opinion, $('#portfolio_cycler .active') is not a good way to get the element because jQuery's sizzle engine parses the selector right to left. So the first thing it does, is grab EVERY ELEMENT on the page and filter out all the ones that don't have a class of active. Once the engine has an element or elements it then has to go through every ancestor of that element until it finds one with an id of portfolio_cycler or it hits the document root, at which point that element is excluded.

    A much more efficient selector would be:

    //or, in the case of the HTML code that has been referenced the following is good enough
    //this is because all of the children of #portfolio_cycler are img's anyway
    This would firstly go right to a native JavaScript DOM method (getElementById) to grab #portfolio_cycler and then look at it's children that are img tags and grab the element(s) that have a class of active.

    The way the image slider you referenced works is that it FADES OUT the top image, it doesn't fade in the next one. So basically, it z-indexes the images so that the current one is at the highest level and then, when it needs to move to the next image, it just fades out the current one (revealing the next image underneath it).
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...

  3. #3
    Join Date
    Feb 2011
    thanks, it is much clearer now!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.28995 seconds
  • Memory Usage 2,867KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (8)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates