简体   繁体   English

在运行时检测javascript文本或元素溢出

[英]Detecting javascript text or element overflow at run time

I am designing a website its design is something like this. 我正在设计一个网站,它的设计是这样的。

网站设计

In this site i have disabled horizontal scrolling instead a User have to scroll using Arrow Key Left and Right to view pages. 在这个网站中,我禁用了水平滚动而不是用户必须使用向左和向右箭头键滚动才能查看页面。

Now to support this feature i have to check for text/element overflow-y each time a new page is loaded in window and if it is causing the overflow then I had to cut and move the overflow element to next page to support horizontal scrolling. 现在为了支持这个功能,每次在窗口中加载新页面时都必须检查文本/元素溢出 - 如果它导致溢出,那么我不得不剪切并将溢出元素移动到下一页以支持水平滚动。

And to support Responsive design I am not adding these pages statically instead I am creating these pages dynamically so that in case width or height of window changes the next pages which will be displayed will adjust to changed width and height accordingly by checking its overflow. 并且为了支持响应式设计,我不是静态地添加这些页面,而是我动态地创建这些页面,以便在窗口的宽度或高度改变的情况下,将显示的下一页将通过检查其溢出来相应地调整为改变的宽度和高度。

Now the problem I am having is that it is very difficult to apply slide effect animation to this design because to apply slide effect we have to know in advance what will be our next element be to replace that slide but in this case our next element is getting added dynamically and to show the next page i will have to first load that element to window so that it first checks the overflow and creates page and then only page will be displayed. 现在我遇到的问题是将幻灯片效果动画应用到这个设计是非常困难的,因为要应用幻灯片效果我们必须提前知道我们的下一个元素是替换幻灯片,但在这种情况下我们的下一个元素是动态添加并显示下一页我将首先将该元素加载到窗口,以便它首先检查溢出并创建页面,然后只显示页面。

Is there any other way to check for element causing overflow ? 有没有其他方法来检查导致溢出的元素?

Note: On turning to next page by Arrow Key all 3 pages which are displaying on window are getting changed. 注意:在通过箭头键转到下一页时,窗口上显示的所有3个页面都会发生变化。

code I am using to check for overflow:- 我用来检查溢出的代码: -

OverflowY: function(parentObj){

    if(parentObj.offsetHeight < parentObj.scrollHeight){
        //Return true if overflow occurs
        return true;
    }
    else{
        return false;
    }

}

The nice way to do this would be to have your columns automatically adjust using strictly HTML and CSS. 这样做的好方法是使用严格的HTML和CSS自动调整列。 This would mean you could just render your content, and the browser would take care of the rest. 这意味着你可以只渲染你的内容,浏览器会处理剩下的事情。 I found a blog article that attempts to solve this problem ( http://alistapart.com/article/multicolumnlists ). 我发现了一篇试图解决这个问题的博客文章( http://alistapart.com/article/multicolumnlists )。 There's probably some good ideas you can glean from there. 你可以从那里收集一些好的想法。 Original stackoverflow post also has good questions: Wrapping lists into columns . 原始的stackoverflow帖子也有很好的问题: 将列表包装成列

If that doesn't fit your use case, a common approach for verifying calculated dimensions of elements is to first render the element somewhere outside the visible window (eg, left: -9999px) within a container of the current dimensions you'd like to render it in. Then you can check the rendered dimensions and adjust the markup as you see fit. 如果这不适合您的用例,验证元素的计算尺寸的常用方法是首先将元素呈现在您想要的当前尺寸的容器内的可见窗口之外的某处(例如,左:-9999px)渲染它。然后你可以检查渲染的尺寸并根据需要调整标记。 Then lastly, clone/detach your modified DOM element and insert it into the visible window in the correct location. 最后,克隆/分离修改后的DOM元素并将其插入到正确位置的可见窗口中。

-insert a div to the page with css properties {visibility:hidden; - 使用css属性将一个div插入到页面中{visibility:hidden; event-pointers: none} to make the div invisible and prevent it to respond to click/touch event. event-pointers:none}使div不可见并阻止它响应click / touch事件。

-use that div to measure a page. - 使用div来测量页面。 I will suggest to loop trough the tag. 我建议循环使用标签。 DOM document manipulation is a very performance cost. DOM文档操作是一项非常高的性能成本。

-insert that page to an array with an ID which specifies its reference number(page number). - 将该页面插入具有ID的数组,该ID指定其引用号(页码)。

-then get the current page id, and loop trough your array to get the previous(left) or next (right) page's index. - 然后获取当前页面ID,并循环到您的数组以获取上一页(左)或下一页(右页)的索引。

-insert that page in the DOM document with something like that : For next page: pgs[nextPage].style.right="-100%”; For previous page: pgs[prePage].style.right="100%”; - 在DOM文档中插入该页面,如下所示:对于下一页:pgs [nextPage] .style.right =“ - 100%”;对于上一页:pgs [prePage] .style.right =“100%”;

PageContainer.appendChild( pgs[nexgPage] ); PageContainer.appendChild(pgs [nexgPage]);

-use jquery.js or transint.js (that will help avoiding browser prefix issues for non-standarized rule): $(pgs[next/prePag]).transition({ right:0}); -use jquery.js或transint.js(这将有助于避免非标准化规则的浏览器前缀问题):$(pgs [next / prePag])。transition({right:0});

-remove the "previous current" page as well if you want". You might have z-index issue (ie the sliding page is under the current page and prevent the effect to be viewed). So make sure you set a lower z-index value for the currently displayed page too. - 如果你想要的话,也可以删除“上一个当前”页面。你可能有z-index问题(即滑动页面位于当前页面下并阻止查看效果)。所以一定要设置较低的z-当前显示页面的索引值。

Though, css' rows are simpler and better for performance (row-width:100%) as already suggested. 尽管如此,css'行更简单,性能更好(行宽:100%)。

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

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