简体   繁体   English

如何随着页面宽度增加自动水平滚动

[英]How to automatically scroll horizontally as page width increase

I am building a website that expands horizontally as user takes action like http://portal.azure.com style. 我正在建立一个网站,该网站会随着用户采取类似http://portal.azure.com样式的操作而水平扩展。 When they click a button(from a list) in one div, the details of the selected items appear in another div beside it. 当他们单击一个div中的按钮(从列表中)时,所选项目的详细信息将显示在旁边的另一个div中。 this can get really long and over flow the mother div. 这可能会变得很漫长,并且使母亲div溢出。

I am looking for a way i can automatically scroll the page to the right most edge when a new div overflows. 我正在寻找一种方法,当新的div溢出时,我可以自动将页面滚动到最右边。

layout 布局

<div style="overflow-x: auto">
  <div layout="row">
    <div class="col" style="width: 400px">
    </div>

    //SHOWN DYNAMICALLY
    <div class="col" style="width: 400px">
    </div>

    //SHOWN DYNAMICALLY
    <div class="col" style="width: 400px">
    </div>

    //SHOWN DYNAMICALLY
    <div class="col" style="width: 400px">
    </div>

    //SHOWN DYNAMICALLY
    <div class="col" style="width: 400px">
    </div> 
  </div>
</div>

As you can see above, the first div shows by default but the other divs appear based on user interaction. 如上所示,默认情况下会显示第一个div,但其他div会根据用户互动显示。 By the time the 3 div appears, it overflows. 到3格出现时,它溢出了。 How can i scroll to the right edge anytime it over flows? 每当流过时如何滚动到右边? (you should really check out http://portal.azure.com to see what im talking about) (您应该真正查看http://portal.azure.com以了解即时通讯的内容)

PS: i am using AngularJS. PS:我正在使用AngularJS。 I am not using jquery. 我没有使用jQuery。 But i dont mind including it if its the only option 但是我不介意包括它,如果它是唯一的选择

You can use plain Javascript for keeping the scroll to right. 您可以使用纯Javascript来保持向右滚动。

Something like this: 像这样:

var myDiv = document.getElementById("row");
myDiv.scrollLeft = myDiv.scrollWidth;

You need to fire the above function every time you add a new div . 每次添加新的div时,都需要触发上述功能。 That way it will always automatically be scrolled when div s are dynamically added. 这样,在动态添加div时,它将始终自动滚动。

You will need to hook up the DOMNodeInserted event on your container. 您将需要在容器上连接DOMNodeInserted事件。 The function will be called whenever a div is added to your row container. 每当将div添加到row容器中时,就会调用该函数。 This way you will not have to change anything in your existing code. 这样,您将不必更改现有代码中的任何内容。

Here is a very simple example with dynamically added div s: 这是一个动态添加div的非常简单的示例:

 var num = 1, btn = document.getElementById('btn'), row = document.getElementById("row"); scroller(); // fire teh scroller right away for initial scroll // function to keep it scrolled towards right // function scroller() { row.scrollLeft = row.scrollWidth; } // edited to add simple animation function scroller() { var maxScroll = row.scrollWidth - row.clientWidth; // required to stop row.scrollLeft += 2; if (row.scrollLeft < maxScroll) { timer = window.setTimeout(scroller, 1000 / 60); } } // hook up event to call scroller whenever an element is dynamically added row.addEventListener("DOMNodeInserted", scroller); // for demo to simluate dynamically adding divs btn.addEventListener("click", function() { var newDiv = document.createElement("div"); newDiv.setAttribute("class", "col"); num += 1; newDiv.innerText = num; row.appendChild(newDiv); }); 
 div[layout] { width: 500px; height: 140px; white-space: nowrap; overflow: hidden; overflow-x: auto; } div.col { height: 140px; width: 400px; display: inline-block; text-align:center; } div { border: 1px solid red; } 
 <div id="row" layout="row"><div class="col">1</div></div> <button id="btn">Add</button> 

Edit : Added simple animation using setTimeout (in order to keep jQuery away). 编辑 :使用setTimeout添加了简单的动画(以使jQuery远离)。 Ideally you should be using requestAnimationFrame or a suitable library if you are already using one. 理想情况下,如果已经使用了requestAnimationFrame应该使用requestAnimationFrame或合适的库。

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

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