0

I have written a jquery code for a pop up but the problem is I wanted to optimize the jquery code can you tell me how to optimise it to have a better results in the browser..

it would be great if u tell for html and css too....

I am providing jquery code below....

    // locale selector actions
    $('#region-picker').click(function(){
        if ($("#locale-select").is(":visible")) return closeSelector('slide');
        var foot_height = $('#footer').innerHeight();
        var foot_height_css = foot_height-1;
        var select_position = '-=' + (Number(700)+18);
        console.log("hallo"+select_position);
        var $selector = $('#locale-select');
        $('#locale_pop').fadeOut();
        $selector.css({top:foot_height_css});
        $selector.fadeIn(function(){
            $(this).addClass('open');
            $(this).animate({top:select_position}, 1000);
            });
    });
    $('#select-tab').click(function(e){
        e.stopPropagation()
        closeSelector('slide');
        });
    // don't hide when clicked within the box
    $('#locale-select').click(function(e){
        e.stopPropagation();
    });
    $(document).click(function(){ 
        if ($('#locale-select').hasClass('open')) {
            closeSelector('disappear');
        }
    });

    $('.locale-link').click(function(){
        //var $clicked = $(this); //"$(this)" and "this" is the clicked span
        $(".locale-select-lable").html($(this).html());
        //search for "marginXXXXX"
        var flags = $(this).attr("class").match(/(margin\w+)\s/g);
        //create new class; add matching value if found
        var flagClass = "tk-museo-sans locale-select-lable" + (flags.length ? " " + flags[0] : "");
        //set new class definition
        $(".locale-select-lable").attr("class", flagClass);

        closeSelector('disappear');

        //if ($("#locale-select").is(":visible")) return closeSelector('slide');
       /*         
       // var desired_locale = $(this).attr('rel');
       // createCookie('desired-locale',desired_locale,360);
       // createCookie('buy_flow_locale',desired_locale,360);
        //closeSelector('disappear');
        */
    });  /* CORRECTED */

    $('#locale_pop a.close').click(function(){
        var show_blip_count = readCookie('show_blip_count');
        if (!show_blip_count) {
            createCookie('show_blip_count',3,360);
        }
        else if (show_blip_count < 3 ) {
            eraseCookie('show_blip_count');
            createCookie('show_blip_count',3,360);
        }
        $('#locale_pop').slideUp();
        return false;
    }); 

function closeSelector(hide_type){
var foot_height = $('#footer').innerHeight();
var select_position = '+=' + (Number(400)+20);
if (hide_type == 'slide') {
$('#locale-select').animate({top:select_position}, 1000, function(){
$(this).removeClass('open');
$(this).fadeOut()
});
}
else if (hide_type == 'disappear'){
$('#locale-select').fadeOut('fast');
$('#locale-select').removeClass('open');
}
}

1 Answers1

0

Most of the standard jquery optimizations can be easily found googling jquery best practices. Lots of resources. Here are a couple from google.

jQuery Standards and Best Practice

http://24ways.org/2011/your-jquery-now-with-less-suck

Specifically for your code I'd look at using .on() instead of .click() for your .locale-link elements and also take advantage of method chaining and caching some jquery objects here and there. Hope this helps a bit.

Community
  • 1
  • 1
brains911
  • 1,310
  • 7
  • 5