简体   繁体   English

如何使用 javascript 或 jQuery 动态更改 Bootstrap 5 下拉菜单的偏移量?

[英]How can I dynamically change the offset of a Bootstrap 5 dropdown menu using javascript or jQuery?

My web app includes dynamically built Bootstrap 5 dropdown menus that can be quite large.我的 web 应用程序包含动态构建的 Bootstrap 5 下拉菜单,这些菜单可能非常大。 They need to be resized and repositioned to make the best use of screen space.它们需要调整大小和重新定位,以充分利用屏幕空间。 So I'd like to be able to control or adjust the offset computed by Popper.js via javascript. But I haven't been make this work.所以我希望能够通过 javascript 控制或调整Popper.js 计算的偏移量。但我还没有完成这项工作。 The Popper.js documentation is not so clear, and I haven't found any documentation or examples of how to do this within Bootstrap 5. (The data-bs-offset attribute isn't suitable since it cannot invoke a function to provide dynamic offset values.) Popper.js 文档不是很清楚,我还没有找到任何文档或示例说明如何在 Bootstrap 5 中执行此操作。( data-bs-offset属性不合适,因为它无法调用 function 来提供动态偏移值。)

Please see the code example on JSFiddle https://jsfiddle.net/Martin_ATS/cqw5mjLv/3/ .请参阅 JSFiddle https://jsfiddle.net/Martin_ATS/cqw5mjLv/3/上的代码示例。 I am able to access the Dropdown object both via bootstrap.Dropdown.getInstance() and via jQuery $(ddelem).dropdown() , as demonstrated in the hideDropdown() function. But I have not been able to change the offset via javascript despite a number of attempts at calling setOptions() etc.我可以通过bootstrap.Dropdown.getInstance()和 jQuery $(ddelem).dropdown()访问 Dropdown object,如hideDropdown() function 所示。但我无法通过 javascript 更改偏移量尽管多次尝试调用setOptions()等。

I'd like to use Bootstrap/Popper's computed y offset, but adjust the x offset so that the menu opens closer to the left margin, and ideally leaves a right margin instead of hitting the right side of the window. The code snippet is a simplified example.我想使用 Bootstrap/Popper 计算的y偏移量,但调整x偏移量,使菜单打开时更靠近左边距,理想情况下留出右边距而不是碰到 window 的右侧。代码片段是简化的例子。 In the real app, the dropdown menu contents can range from small (for which Bootstrap/Popper's normal offsets work fine) to very large (including horizontal or vertical scrolling).在真实的应用程序中,下拉菜单内容的范围可以从小(Bootstrap/Popper 的正常偏移工作正常)到非常大(包括水平或垂直滚动)。

Thanks!谢谢!

Get the instance of the Popper (which is _popper in the dd instance), and then set the offset inside the modifers array...获取 Popper 的实例(在 dd 实例中为 _popper),然后在 modifers 数组中设置偏移量...

        var dd = bootstrap.Dropdown.getInstance(evt.relatedTarget);
        console.log(`shown.bs.dropdown ${evt.relatedTarget.id}`);

        var $ddelem = $(evt.relatedTarget);
        dd._popper.setOptions( {modifiers: [
             {
                 name: 'offset',
                 options: {
                   // calculate and set the offset as needed here..
                   offset: [30, 10],
                 },
             },
        ]})

Demo演示

暂无
暂无

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

相关问题 如何使用 Bootstrap 4 更改下拉菜单活动背景颜色? - How can I change dropdown menu active background color with Bootstrap 4? 如何在JavaScript中动态创建下拉菜单列表? - How can I dynamically create an dropdown menu list in JavaScript? 如何使用JavaScript中的classList.contains在引导程序下拉菜单中获取li类 - how can I get the li classes inside the bootstrap dropdown menu using the classList.contains from javascript 当我使用jQuery单击另一个下拉菜单按钮时,如何关闭活动的下拉菜单? - How can I close an active dropdown menu when I click on another dropdown menu button using jQuery? 如何使用jQuery突出显示引导程序下拉菜单的活动菜单? - How to highlight active menu of a dropdown menu of bootstrap using jquery? 如何使用jQuery在引导程序中动态设置弹出偏移 - How to dynamically set popover offset in bootstrap using jquery 如何通过使用 javascript 在外部单击来隐藏下拉菜单 - how can i hide dropdown menu by clicking outside using javascript 如何使用 javascript 将引导警报动态添加到页面? - How can I dynamically add bootstrap alerts to a page using javascript? 如何使用 JavaScript 或 jQuery 动态更改时间(纯文本)? - How can I dynamically change the time (which is plain text) using JavaScript or jQuery? 使用jquery或javascript动态更改使用bootstrap的选项? - Using jquery or javascript to dynamically change select options using bootstrap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM