2

I currently have the following code.

to get the #headerfader to resume running after the second function runs (hovering over and off again to reset the fading text)

I've duplicated the code inside the function.

Is there a more elegant way of handling this?

//FADER TEXT
        $('#headerFader').carousel({
            interval: 2500,
            pause: false
        });

        //BACK HOME TEXT
       $('#headerText').hover(
           function(){
               $(this).find("h1#masterHeader").animate({opacity:0}, 0, function(){
                   $(this).css("display", "none");
                   $('#headerText').find("h1#takemeback").css("display", "block");
                   $('#headerText').find("h1#takemeback").animate({opacity:1,});
               });
           },
           function(){
               $(this).find("h1#takemeback").animate({opacity:0}, 0, function(){
                   $(this).css("display", "none");
                   $('#headerText').find("h1#masterHeader").css("display", "block");
                   $('#headerText').find("h1#masterHeader").animate({opacity:1,});
                   //FADER TEXT
                    $('#headerFader').carousel({
                        interval: 2500,
                        pause: false
                    });

               });
           }
       );
4
  • 4
    Code duplication can be avoided by moving the duplicate code into its own function and call that function instead. To learn more about functions, have a look at the MDN JavaScript guide: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions Commented Mar 19, 2015 at 16:17
  • 1
    Also, you should seriously consider caching your jQuery objects into a variable. Commented Mar 19, 2015 at 16:18
  • This is code I've inherited from another developer, I'm currently attempting to fix bugs and improve the code as I go along. Cheers. Commented Mar 19, 2015 at 16:20
  • @SterlingArcher to be fair, recent versions of JQuery mitigate performance issues now by caching your selectors internally. However you still do have nanoseconds of overhead for having to make the function call in the first place, but it's definitely not the performance bottleneck it used to be. Commented Mar 19, 2015 at 16:24

2 Answers 2

3

This is a complex solution to this problem, which minimizes the numbers of jQuery element fetching calls

Note that it's clean and a lot faster than the other solutions provided. Unless it's tested I believe that this is a working piece of code.

Since you are using # div id selector (which is unique) the is no need for calling find() function.

//Object handles
var headerFader  = $('#headerFader');
var masterHeader = $('#masterHeader');
var takemeback   = $('#takemeback');

headerFader.carousel({
    interval: 2500,
    pause: false
});

//BACK HOME TEXT
headerFader.hover(
    function(){
        masterHeader.animate({opacity:0}, 0, function(){
            $(this).css("display", "none");
            takemeback.css("display", "block").animate({opacity:1}, 1000);
        });
    },
    function(){
        takemeback.animate({opacity:0}, 0, function(){
            $(this).css("display", "none");
            masterHeader.css("display", "block").animate({opacity:1}, 1000);
            headerFader.carousel({
                interval: 2500,
                pause: false
            });
        });
    }
);
Sign up to request clarification or add additional context in comments.

5 Comments

"Unless it's tested I believe that this is a working piece of code." Story of my life.
Force author to provide html code along with css and my WebStorm is ready to fire. This is a very good solution + programming practice lesson. Your comment is unfair to me.
Yeah, sorry I got frustrated over this one, it was already marked as solved with solution that had 3 major errors, later some guys provided some twisted solutions that forced me to fix it once and for all.
I was just joking about the way you structured your grammar, making it sound like "This code works, unless you test it." And I was saying that is the story of my life :P Your solution is fine :)
Yeah sorry, I would have provided the html / css but I'm new to MVC so I had no idea where the code was. I'll plug it in on Monday and get it working.
2

Outside your other functions, you'd do this:

function showHeader(el) {
    $(el).css("display", "none");
    $('#headerText').find("h1#takemeback").css("display", "block");
    $('#headerText').find("h1#takemeback").animate({opacity:1,});
}

Then, inside your hover functions, call it:

showHeader(this);

Along with the variable suggestion above, you can chain your statements:

$('#headerText').find("h1#takemeback").css("display", "block").animate({opacity:1,});

Also, #takemeback should be unique, so this would do:

$("#takemeback").css("display", "block").animate({opacity:1,});

6 Comments

Cheers, thank you. I was also wondering about the $('#headerFader').carousel({ interval: 2500, pause: false }); Would it be worth it to stick that as it's own function too?
Purists would tell you to abstract nearly everything that can be. So, yes. :)
This is not 100% valid, you still REPEAT the $( ) and find( ) call.
Still not valid in his particular example.. I don't know who marked it as solve, it's miss leading.
How do you handle "h1#masterHeader" and "h1#takemeback" differentiation in hover( inFunc, outFunc ) ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.