jquery动态设置banner高度,确保banner高度为图片高度

时间:2020-05-07 10:21:01   收藏:0   阅读:60

 

$(function(){
    // 刚开始这么写的,但是h高度偶尔还是会被设置为0
// $(".banner .bd img").load(function(){ // var h = $(".banner .bd img").height(); // console.log(‘h:‘,h); // $(".banner").css("height",h); // })
//修改后,用一下方式设置banner高度,实现效果
$(".banner .bd img").on("load",function(){ var img=new Image(); img.src=this.src; // console.log(‘屏幕宽,图片宽,图片高:‘,document.body.scrollWidth ,img.width,img.height); var h = (img.height * document.body.scrollWidth) / img.width; // console.log(‘h3:‘,h); $(".banner").css("height",h); });
$(window).resize(
function() { var h = $(".banner .bd img").height(); $(".banner").css("height",h); }); })

 

原文:https://www.cnblogs.com/duanzhenzhen/p/12841195.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!