Site icon Anthony Carbon

Custom Jquery collections

Navigation menu on-click prevent default if menu item has children and fires only on second click

// OPTION 1

// JQUERY
$( '.menu-item-has-children a' ).click( function(e){ 
    $( this ).parent().toggleClass( 'active-li' );
    if( $( this ).next( '.sub-menu' ).css( 'display' ) == 'block' ){ 			
        e.preventDefault();		
    } 
});
// CSS
ul.menu .sub-menu { display: none; }
ul.menu .active-li .sub-menu { display: block; }

// OPTION 2

$( 'ul.mobile-menu li a' ).click( function( e ){
    if( $( this ).parent().find( '.sub-menu:first' ).css( 'display' ) == 'none' ){
        e.preventDefault();
        $( this ).parent().siblings().find( '.sub-menu:first' ).slideUp(function() {
            $(this).parent().toggleClass( 'menu-open' );
        });
        $( this ).parent().find( '.sub-menu:first' ).slideToggle(function() {
            $(this).parent().toggleClass( 'menu-open' );
        });
    }else{
        return true;
    } 
});

Get the total width of all li tags

Scroll top

<a id="toTop" class="scrollToTop" href="#" style="display: inline; opacity: 1;">To Top</a>
$( window ).scroll(function(){
    if ( $(this).scrollTop() > 300 ) {
        $( '.scrollToTop' ).animate({'opacity' : 1},100);
    }else{
        $( '.scrollToTop' ).animate({'opacity' : 0},50);
    }  
}); 
$('.scrollToTop').click(function(){
    $('html, body').animate({scrollTop : 0},1300);
    return false;
});

On first load sessions

if (document.cookie.indexOf('visited=true') == -1) {
    var fifteenDays = 1000*60*60*24*15;
    var expires = new Date((new Date()).valueOf() + fifteenDays);
    document.cookie = "visited=true;expires=" + expires.toUTCString();
}
var all_width = 0; 
jQuery( '.nav-primary .menu' ).children().each( function( index, element ){ 
    all_width += jQuery( this ).innerWidth();    
});	

Remove ‘px’ in 13.3999996185303px

var str = '13.3999996185303px';
str.replace( 'px', '' ); // Output will be 13.3999996185303
// or
parseFloat( str ) // Output will be 13.3999996185303
this.addEventListener("load", loadTimer, true); 
function loadTimer(e) {
    count = $(".MagicScrollItem").children().length;
    var total_width = count*265;
    jQuery('.MagicScrollItem').parent().addClass('scroll-wrapper'); 
    jQuery('.scroll-wrapper').css({width: total_width+"px"}); 
}

Detect your current computer resolutions

screen.width + ' x ' + screen.height

or window.innerWidth

Fire the script after the window is resize

var custom_timeout_function = ( function () {
      var timers = {};
      return function (callback, ms, uniqueId) {
        if (!uniqueId) {
              uniqueId = "Don't call this twice without a uniqueId";
        }
        if (timers[uniqueId]) {
              clearTimeout (timers[uniqueId]);
        }
        timers[uniqueId] = setTimeout(callback, ms);
      };
})(); 
custom_timeout_function( function(){  
    jQuery( '.woocommerce-tabs ul.tabs li a' ).each( function(){  
        jQuery( this ).animate({ 'padding-right': _padding+'px', 'padding-left': _padding+'px' }, 300);
    });
    jQuery( '.woocommerce-tabs ul.tabs' ).css({ 'float': 'none' });
}, 900 ); 

Add / Remove class onclick in the same element

    var click_count = 1;
    $(".responsive-menu span.has-children").click(function(event){  
        if( click_count %2 == 0 ){
            $(this).removeClass('active-children');   
        }else{
            $(this).addClass('active-children');  
        }
        $(this).parent().children( 'ul' ).slideToggle(); 
        click_count++;
    });
Exit mobile version