当将手风琴用于大窗格时,选择窗格将带您进入其底部内容,而将顶部内容置于屏幕的可见范围之外。选择窗格时,强制用户每次向上滚动。选择窗格时是否可以自动滚动到窗格内容的顶部?此处提供手风琴:https://jsfiddle.net/4hqv4eao/4/require([ "dijit/layout/AccordionContainer", "dijit/layout/ContentPane", "dojo/ready" ], function(AccordionContainer, ContentPane, ready) { ready(function() { var acc = new AccordionContainer({}, "accordion"); acc.addChild(new ContentPane({ title: "Title 1", }, "pane1")); acc.addChild(new ContentPane({ title: "Title 2", }, "pane2")); acc.addChild(new ContentPane({ title: "Title 3", }, "pane3")); acc.addChild(new ContentPane({ title: "Title 4", }, "pane4")); acc.startup(); }); }); 最佳答案 使用每个窗格的单击功能(我使用了for loop和className dijitAccordionInnerContainer),可以获取手风琴的y位置(我使用了here中的函数来获取确切的y位置)并使用javascripts 。这是获取确切位置的函数(感谢kirupa):// Helper function to get an element's exact positionfunction getPosition(el) { var xPos = 0; var yPos = 0; while (el) { if (el.tagName == "BODY") { // deal with browser quirks with body/window/document and page scroll var xScroll = el.scrollLeft || document.documentElement.scrollLeft; var yScroll = el.scrollTop || document.documentElement.scrollTop; xPos += (el.offsetLeft - xScroll + el.clientLeft); yPos += (el.offsetTop - yScroll + el.clientTop); } else { // for all other non-BODY elements xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft); yPos += (el.offsetTop - el.scrollTop + el.clientTop); } el = el.offsetParent; } return { x: xPos, y: yPos };}这是我添加到滚动到手风琴顶部的代码:var panes;var pane;panes = document.getElementsByClassName('dijitAccordionInnerContainer');for (var i = 0; i < panes.length; i++) { panes[i].addEventListener('click', function(e) { pane = this; setTimeout(function() { window.scrollBy(0, getPosition(pane).y); }, 200); });}我使用了setTimeout函数,因此手风琴将在获得其位置之前完成更新(这样做可能是更好的方法)。这是scrollBy method。关于javascript - Accordion :自动滚动到 Pane 内容的顶部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48344600/ 10-15 08:38