简体   繁体   English

溢出内容的宽度

[英]Width of overflowed content

I'm working on a tag scroller that will basically allow users to scroll through chunks of tags left or right if there are more tags than fit their current containing div. 我正在研究一个标签滚动器,该标签滚动器基本上允许用户在标签数量超出其当前包含的div范围的情况下向左或向右滚动标签块。 My plan was to have the component div set to overflow:hidden so the tags (and their parent div) would not wrap. 我的计划是将组件div设置为overflow:hidden,以便标记(及其父div)不会自动换行。 Then I'd have left and right arrows that would animate the tag wrapper to the left or right. 然后,我需要使用向左和向右的箭头来使标签包装器动画化。

I need to determine if the width of the tag-wrapper is greater than the tag-scroller itself. 我需要确定标签包装机的宽度是否大于标签包装机本身的宽度。 If so, then I know that there are more tags than fit within the tag-scroller and I should make the arrows visible so a user can click to scroll and view some more. 如果是这样,那么我知道标签滚动器中的标签超出了容纳的范围,我应该使箭头可见,以便用户可以单击以滚动并查看更多内容。 The layout and everything looks as expected however my problem is, using $('.tag-wrapper').width(); 布局和一切看起来都像预期的一样,但是我的问题是,使用$('。tag-wrapper')。width(); always returns a different value depending on window width which shouldn't be the case since the actual content hasn't changed. 总是根据窗口宽度返回不同的值,因为实际的内容没有改变,所以不应该这样。 If the screen is wide enough, I may not need to show the arrows so I need to check the width on window resize, etc. 如果屏幕足够宽,则可能不需要显示箭头,因此需要检查窗口调整大小等的宽度。

Any ideas why $('.tag-wrapper').width(); 任何想法为什么$('。tag-wrapper')。width(); would give me different sizes based on the actual window width even thought the scrollable content itself hasn't changed? 即使认为可滚动内容本身没有变化,也会根据实际窗口宽度给我不同的尺寸?

Here is my markup: 这是我的标记:

  .tag-scroller { overflow: hidden; white-space: no-wrap; display: block; width: 100%; height: 50px; } .tag-wrapper { white-space: nowrap; display: inline-block; border: 1px solid green; } .tag { display: inline-block; width: initial; text-align: center; padding: 5px 10px 6px 10px; } 
 <div class="tag-scroller"> <div class="tag-wrapper"> <div class="tag"></div> <div class="tag"></div> <div class="tag"></div> <div class="tag"></div> </div> </div> 

I used the jQuery outerWidth method. 我使用了jQuery outsideWidth方法。

 $(function() { $(document).on('DOMNodeInserted DOMNodeRemoved', '.tag-wrapper', function(e) { $('#p').text('tag-wrapper width: ' + $('.tag-wrapper').outerWidth() + 'tag-scroller width: ' + $('.tag-scroller').outerWidth()); if ($('.tag-wrapper').outerWidth() > $('.tag-scroller').outerWidth()) { alert('ok'); } }); $('#btn').click(function(e) { $('.tag-wrapper').append($('<div class="tag">1</div>')); }); $('#btnWidth').click(function(e) { $('#p').text('tag-wrapper width: ' + $('.tag-wrapper').outerWidth() + 'tag-scroller width: ' + $('.tag-scroller').outerWidth()); }); }); 
 .tag-scroller { overflow:hidden; white-space:no-wrap; display:block; width:100%; height:50px; } .tag-wrapper { white-space: nowrap; display:inline-block; border:1px solid green; } .tag { display:inline-block; width:initial; text-align:center; padding: 5px 10px 6px 10px; } 
 <script src="http://code.jquery.com/jquery-1.11.3.js"></script> <div class="tag-scroller"> <div class="tag-wrapper"> <div class="tag">1</div> <div class="tag">2</div> <div class="tag">3</div> <div class="tag">4</div> </div> </div> <button id="btn">Click Me</button> <button id="btnWidth">Check Widths</button> <p id="p"></p> 

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

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