Scroll position of a jquerymobile collapsible set when expanded

允我心安 提交于 2019-12-19 07:18:44

问题


I am using a jQueryMobile (v1.4.0) collapsible set / accordions to display a list of elements and its content as shown in this jsFiddle.

<div id="List" data-role="collapsible-set">
    <div data-role="collapsible" data-content-theme="c">
       <h3>Lorem ipsum 1</h3>
       <p>Suspendisse neque...</p>
    </div>
    <div data-role="collapsible" data-content-theme="c">
       <h3>Lorem ipsum 2</h3>
       <p>Lorem ipsum...</p>
    </div>
</div> 

The problem I have is with scrolling when the content of an item is longer than the length of the screen.

For instance in the fiddle:

  • Open the first collapsible item
  • Scroll to the bottom (if you do not have to scroll, resize the window so that you have to... otherwise the problem isn't visible)
  • Open the second item

=> the first item is closed and the second item is opened, but the page scrolling doesn't change and you now see the end of the second item's content.

Thus my question: Is there a smart way to force the page to set the "header" of the second item at the top of the screen?

Thanks, T.


回答1:


Once a collapsible is expanded, retrieve its' .offset().top and $.mobile.silentScroll() to that position.

$(document).on("expand", "[data-role=collapsible]", function () {
  var position = $(this).offset().top;
  $.mobile.silentScroll(position);
});

Update: For jQuery Mobile 1.4, use collapsibleexpand event.

Demo - jQM 1.0 - 1.1

Demo - jQM 1.2 - 1.3

Demo - jQM 1.4



来源:https://stackoverflow.com/questions/20685256/scroll-position-of-a-jquerymobile-collapsible-set-when-expanded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!