javascript - 用于检测高度和更改填充的脚本

标签 javascript jquery html css

我有一大块文字。文本 block 中有一个标题。它可以是 25 或 50 像素高,具体取决于它是 1 行还是 2 行。标题是 div 内的一个 p 标签,具有 height: auto; 的 css 方面。我正在使用脚本来检测 div 的高度。默认情况下,div 为 1 行高 25。如果是这样,什么都没有改变。如果它是 50 像素高,它应该包括样式标签,其中一些仍在内部更改不同潜水移动文本的填充。我目前正在运行此脚本(它不起作用,这就是我请求帮助的原因):

<script>
$(document).ready(function () {
	if ($('.rtitle').height() == 50) {
	document.write("<style>
    div#raffleinfo {
    	height: 127px;
    	padding-top: 173px;
    }</style>)";
	}
});
</script>
div#raffledisplay {
	height: 300px;
	width: 100%;
	position: relative;
}

div#raffleinfo {
	float: left;
	width: 480px;
	height: 102px;
	padding-top: 198px;
	background-color: red;
}

div#rtitle {
	height: auto;
}

p.rtitle {
	font-size: 35;
	line-height: 25px;
}

p.rinfo {
	font-size: 25;
	line-height: 25px;
}

#progressBar {
	width: 478px;
	height: 25px;
	border: 1px solid #111;
	background-color: #292929;
}
<div id="raffledisplay">
	<div id="raffleinfo">
		<div id="rtitle"><p class="rtitle">$weapon | $skin $stattrack</p></div>
		<p class="rinfo">Quality: $quality</p>
		<p class="rinfo">Price: $price</p>
		<div id="progressBar" class="pbar"><div></div></div>
	</div>
</div>

我不确定如果你运行代码为什么会显示所有奇怪的东西,但在我的网站上它看起来很好...

最佳答案

试试这个

$(document).ready(function() {

  // console.log($('.rtitle').height());
  if ($('.rtitle').height() == 50) {
    $("div#raffleinfo").addClass('larger');
  }
});
div#raffledisplay {
  height: 300px;
  width: 100%;
  position: relative;
}
div#raffleinfo {
  float: left;
  width: 480px;
  height: 102px;
  padding-top: 198px;
  background-color: red;
}
div#raffleinfo.larger {
  height: 127px;
  padding-top: 173px;
}
div#rtitle {
  height: auto;
}
p.rtitle {
  font-size: 35;
  line-height: 25px;
}
p.rinfo {
  font-size: 25;
  line-height: 25px;
  //background-color: green;
}
#progressBar {
  width: 478px;
  height: 25px;
  border: 1px solid #111;
  background-color: #292929;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="raffledisplay">
  <div id="raffleinfo">

    <div id="rtitle">
      <p class="rtitle">$weapon $skin $stattrack xxxxxxxx xx xxx xx  xxxxxxx xxxxxxxxx xxxxxxx xxxxx </p>
    </div>

    <p class="rinfo">Quality: $quality</p>
    <p class="rinfo">Price: $price</p>

    <div id="progressBar" class="pbar">
      <div>
      </div>
    </div>

关于javascript - 用于检测高度和更改填充的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788898/

相关文章:

php - 表单提交与 Javascript 结合

javascript - react 原生 : using redux saga eventChannel to externalise event listeners from components

javascript - 每当下载文件时,如何让 Firefox 调用扩展中的函数?

javascript - 计算动态html表格的总计和小计

html - 为什么浏览器对 HTML 不严格?

html - 在我的 HTML/CSS 代码上放置一个虚拟的 Facebook 登录按钮

javascript - react 类型错误 : Cannot read property of undefined - But I can read other props just fine

javascript - Jquery - 单击时更改颜色

jQuery 使用声明的位置对对象进行动画处理

html - 内联div不显示