简体   繁体   English

如何使用jQuery检查元素是否中断到下一行?

[英]How to use jQuery to check if element breaks to the next line?

I have a setup that displays two elements side-by-side, specifically an image and a Facebook like box. 我有一个设置,可以并排显示两个元素,特别是图像和类似Facebook的框。

The image has float:left and the Facebook like box div/span has float:right - the HTML code looks something like so: 图像有float:left和Facebook喜欢框div / span有float:right - HTML代码看起来像这样:

<div class="header"><img class="logo" src="logo.jpg"><span class="fb-like-box"><iframe></iframe></span></div>

In certain situations when the browser width shrinks to a certain point, the FB like box will break (or move) to the next line. 在某些情况下,当浏览器宽度缩小到某个点时,FB like框将中断(或移动)到下一行。 I have used CSS @media queries to deal with this. 我使用CSS @media查询来处理这个问题。

Unfortunately , certain mobile browsers do not 'break' or move the FB like box to the next line at the same CSS points. 不幸的是 ,某些移动浏览器不会“破坏”或将FB状框移动到相同CSS点的下一行。 For example it renders fine on a 320px width iPhone and a 360px width Note 2. However, on say the Samsung stock browser with a phone width of 380px, the Facebook like box is displayed on the next line and actually the box itself is rendered horribly. 例如,它在320px宽度的iPhone和360px宽度上呈现良好的注意事项2.但是,如果手机宽度为380px的三星股票浏览器,下一行显示Facebook喜欢的盒子,实际上盒子本身是可怕的。

How do I use jQuery to detect if a floating element is displayed on (or breaks to) the next line and execute a CSS modification? 如何使用jQuery检测浮动元素是否显示在下一行上(或中断)并执行CSS修改?

Thanks for the help! 谢谢您的帮助!

Comparing position().top of both elements on resize or load could trigger a function containing a CSS modification. 比较resize或load position().top的两个元素的position().top可能会触发包含CSS修改的函数。

If the absolute value of the difference between position().top of the left and right elements is greater than a certain amount, and both elements are set to align to the top of their parent element, then it would theoretically mean it's probably a line break. 如果左边和右边元素的position()。top之间的差值的绝对值大于一定量,并且两个元素都设置为对齐到它们的父元素的顶部,那么它理论上意味着它可能是一条线打破。

See jsfiddle example here: https://jsfiddle.net/aprtLumf/2/ 请参阅这里的jsfiddle示例: https ://jsfiddle.net/aprtLumf/2/

jQuery position() documentation: https://api.jquery.com/position/ jQuery position()文档: https//api.jquery.com/position/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM