[英]I want this script to work when page load and also when resize
该网站响应迅速
$(window).bind('resize', function() {
$('#drop_ul').css('width', $('#container').width());
$('#drop_ul').css('left', $('#container').css('margin-left'));
});
$(document).ready(function(){
$('#drop_ul').css('width', $('#container').width());
$('#drop_ul').css('left', $('#container').css('margin-left'));
});
您可以将逻辑提取到其自己的函数中,然后可以在两个事件下调用该逻辑。 尝试这个:
function updateUI() {
$('#drop_ul').css({
'width': $('#container').width(),
'left': $('#container').css('margin-left')
});
}
$(document).ready(function() {
updateUI(); // on load
$(window).on('resize', updateUI); // on window resize
});
请注意,我统一了css()
调用,并使用了on
而不是bind
。
另外,如果网站响应良好,则最好使用CSS媒体查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.