5south 5south - 4 years ago 110
jQuery Question

JQuery: do I need to rewrite a script for window resize function?

$(window).on( 'load' , function() {
var secondspotlightTop = ( ( $( "#under-splash" ).height() - $( "#second-spotlight" ).height() ) * .5 );
$( "#second-spotlight" ).css( "margin-top" , secondspotlightTop );
$(window).resize( function() {
var secondspotlightTop = ( ( $( "#under-splash" ).height() - $( "#second-spotlight" ).height() ) * .5 );
$( "#second-spotlight" ).css( "margin-top" , secondspotlightTop );
} );
} );


Is there a way to name this function and call to it for the window resize function without having to copy and paste it?

Answer Source

Put your code to one function and call inside to window resize function

No need to repeat the code for normal and resize functions

 $(window).on( 'load' , function() {
   function contsize()
      {
                var secondspotlightTop = ( ( $( "#under-splash" ).height() - $( "#second-spotlight" ).height() ) * .5 );
        $( "#second-spotlight" ).css( "margin-top" , secondspotlightTop );
      }
       contsize();
       $(window).bind("resize",function(){contsize();});
    } );

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download