JQuery:我需要重写窗口大小调整功能的脚本吗?

时间:2017-02-02 04:32:31

标签: jquery

    $(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 );
        } );
    } );

有没有办法命名这个函数并调用它来获取窗口大小调整功能,而不必复制和粘贴它?

1 个答案:

答案 0 :(得分:1)

将代码放到一个函数中并调用内部窗口调整大小函数

无需重复正常和调整大小功能的代码

 $(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();});
    } );