Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:36:48+00:00 2026-06-06T07:36:48+00:00

While I know some jQuery to simplify front-end manipulation programming, I also aware of

  • 0

While I know some jQuery to simplify front-end manipulation programming, I also aware of some of some “best practices” to level up the performance on browser, like caching through var and always descend from ID selectors. But I have doubts about this piece of code. Could it be done better?

Preamble: This code animates several blocks inside the (already cached) selector through .hover().

The HTML is something like this:

<div clas="block-link red">(...)</div>
<div clas="block-link yellow">(...)</div>
<div clas="block-link magenta">(...)</div>
<div clas="block-link moradopelusa">(...)</div>

And the jQuery to animate at hovering inside one of them:

var cached_blocks = jQuery(.block-link);

jQuery(cached_blocks).hover(function(){
    var this_block = jQuery(this),
        this_block_text = jQuery(this_block).children(div.text),
        this_block_image = jQuery(this_block).children(div.image),
        this_block_link = jQuery(this_block).children(div.link),
        this_block_link_icon = jQuery(this_block_link).children(a.icon);

    /* Animate in; show everything */
    jQuery(this_block_text).animate(..);
    jQuery(this_block_image).animate(..);
    jQuery(this_block_link).animate(..);
    jQuery(this_block_link_icon).animate(..);

}, function() {
    var this_block = jQuery(this),
        this_block_text = jQuery(this_block).children(div.text),
        this_block_image = jQuery(this_block).children(div.image),
        this_block_link = jQuery(this_block).children(div.link),
        this_block_link_icon = jQuery(this_block_link).children(a.icon);

    /* Animate out; put everything where they belong */
    jQuery(this_block_text).animate(..);
    jQuery(this_block_image).animate(..);
    jQuery(this_block_link).animate(..);
    jQuery(this_block_link_icon).animate(..);
});

I’m seeing that I am declaring variables again after hover, but I don’t know any technique to not doing again. Anyway, even that way it works like a charm.

Update: Fixed Code #1

var cached_blocks = jQuery(.block-link);

jQuery(cached_blocks).on('mouseenter mouseleave', function(){
    var this_block = jQuery(this),
        this_block_text = jQuery(this_block).children(div.text),
        this_block_image = jQuery(this_block).children(div.image),
        this_block_link = jQuery(this_block).children(div.link),
        this_block_link_icon = jQuery(this_block_link).children(a.icon);

    /* Animate in; show everything */
    this_block_text.animate(e.eventType(...));
    this_block_image.animate(e.eventType(...));
    this_block_link.animate(e.eventType(...));
    this_block_link_icon.animate(e.eventType(...));
});

Note 1: Yes, i’m using cached_blocks in other part of the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:36:50+00:00Added an answer on June 6, 2026 at 7:36 am

    You seem to be repeating code for no good reason?

    var cached_blocks = jQuery('.block-link');
    
    cached_blocks.on('mouseenter mouseleave', function(e) {
        var this_block = jQuery(this),
            this_block_text = this_block.children('div.text'),
            this_block_image = this_block.children('div.image'),
            this_block_link = this_block.children('div.link'),
            this_block_link_icon = this_block.children('a.icon');
    
        this_block_text.animate({something: (e.type==='mouseenter' ? 0 : 400)});
        this_block_image.animate({top: (e.type==='mouseenter' ? 10 : 200)});
        this_block_link.animate({left: (e.type==='mouseenter' ? 300 : 40)});
        this_block_link_icon.animate({right: (e.type==='mouseenter' ? 0 : 1400)});
    });
    

    Unless you are using the variables containing the text/image/link… elements somewhere else aswell, caching them just to use with animate() on the next line is a waste of space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to ajax and jquery but I know some php. I
Well, I know that with some jQuery actions, we can add a lot of
Now while I know that you can not perform inheritance like you would in
This one is a huge issue: first off, while I know a little bit
hi all i want to know that while specifying url in my iphone native
Does anybody know what tool used while podcasts were recorded here: http://www.asp.net/mvc/application-development/ For example
want to know why String behaves like value type while using ==. String s1
While user uploading a file, is it possible to know if the uploaded file
It seems Linq2sql doesn't know how to construct the TSQL while you transform linq2sql
I Want to know which one is preferred while coding to use Static Methods

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.