首页 > Web开发 > 详细

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

时间:2020-05-07 10:21:01      阅读:62      评论:0      收藏:0      [点我收藏+]

 

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

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!