$(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 );
} );
} );
有没有办法命名这个函数并调用它来获取窗口大小调整功能,而不必复制和粘贴它?
答案 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();});
} );