With the jQuery accordion control, how can I have it scroll to an item I've selected when it is off the screen?
When:
Is there an option for the accordion to scroll to the second item?
In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element. This behavior can be used to scroll to the top of the page by applying this method on the window property. Setting the position parameter to 0 scrolls the page to the top.
jQuery - Widget accordion The Widget accordion function can be used with widgets in JqueryUI. Accordion is same like as Tabs,When user click headers to expand content that is broken into logical sections.
The jQuery UI Accordion create event is used to trigger when the accordion is created. If an accordion widget is collapsed, then the ui.
Assuming you are using the jQuery UI accordion, you can collapse all sections with . accordion('activate', false) .
2.accordion [0].scrollHeight - find the scroll height of the accordion that is opened. 3.Using the animate option using scrollTop the page is moved to the scroll height of the accordion. 4.To ensure step 2 & 3 are done after the accordion is open the setTimeout function is used to time the code after the accordion is open.
Before clicking the button: After clicking the button: Method 2: Using scrollTo() in jQuery. In jQuery, the scrollTo() method is used to set or return the vertical scrollbar position for a selected element.This behavior can be used to scroll to the top of the page by applying this method on the window property.
1.An accordion variable is set to the parent node with class ".accordion" of the accordion button. 2.accordion [0].scrollHeight - find the scroll height of the accordion that is opened. 3.Using the animate option using scrollTop the page is moved to the scroll height of the accordion.
JavaScript Code: All you have to create a normal Bootstrap Accordion and paste the below JavaScript code int script tag then the action will begin. and REST API, you can not ignore JavaScript.
It works for me and tested,
$( "#accordion" ).accordion({ heightStyle: "content", collapsible: true, active: false, activate: function( event, ui ) { if(!$.isEmptyObject(ui.newHeader.offset())) { $('html:not(:animated), body:not(:animated)').animate({ scrollTop: ui.newHeader.offset().top }, 'slow'); } } });
http://jsfiddle.net/ilyasnone/aqw613em/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With