动态下拉菜单切换方向

发布于 2024-10-12 15:13:23 字数 1823 浏览 3 评论 0原文

我有一个使用 jQuery 的动态下拉菜单,其中主导航链接的数量取决于 CMS。有没有办法做到这一点,如果下拉容器接触网站的包装,那么它会翻转两侧并右对齐,这样主网页容器之外就不会溢出?请参阅 http://online.wsj.com/home-page 作为示例。

编辑

这是代码。

<script>
$("ul#navBar li span").click(function() { //When trigger is clicked...
        //caching selectors
        var $this = $(this),
        $parent = $this.parent(),
        $siblings = $parent.siblings(),
        $dropdowns_siblings = $siblings.find('.dropdown');

        $this.next().toggle(); //toggles the dropdown on click

        //hide other menus if visible
        $dropdowns_siblings.hide();


        function rowWidth(){    
            //Calculate width of ul
            var rowWidth = 0;       
            $parent.find("ul").each(function() {rowWidth += $(this).width(); });    

            //Set width of .dropdown
            $parent.find(".dropdown").css({'width' : rowWidth});
            };
        return rowWidth();
</script>

<div id="container">
 <ul id="navBar">
    <li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
 </ul>

</div>

I have a dynamic dropdown menu using jQuery where the number of main navigation links are dependent on a CMS. Is there way to make it so if the dropdown container touches the wrapper of the website then it flips sides and is right aligned so there is no overflow outside of the main webpage container? See http://online.wsj.com/home-page as an example.

EDIT

Here is th code.

<script>
$("ul#navBar li span").click(function() { //When trigger is clicked...
        //caching selectors
        var $this = $(this),
        $parent = $this.parent(),
        $siblings = $parent.siblings(),
        $dropdowns_siblings = $siblings.find('.dropdown');

        $this.next().toggle(); //toggles the dropdown on click

        //hide other menus if visible
        $dropdowns_siblings.hide();


        function rowWidth(){    
            //Calculate width of ul
            var rowWidth = 0;       
            $parent.find("ul").each(function() {rowWidth += $(this).width(); });    

            //Set width of .dropdown
            $parent.find(".dropdown").css({'width' : rowWidth});
            };
        return rowWidth();
</script>

<div id="container">
 <ul id="navBar">
    <li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
<li><a href="/">Link 1</a> <span></span>
        <div class="dropdown">
            dynamic # of cols
        </div>
    </li>
 </ul>

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2024-10-19 15:13:23

您可以使用 jQuery.fn.offset 并进行比较以查看是否 jQuery(window).width( ) 大于该偏移量的 left 分量与菜单宽度的总和,如果不是并且菜单被裁剪,则更新菜单的位置以对齐本身与按钮(或菜单触发器)的另一边缘等。您可以将此函数绑定为在窗口获取 调整大小或者是否存在水平滚动。很抱歉,如果这太高级并且没有足够的示例代码,但它应该足以让您开始。

...

我已经用 JSFiddle 上的示例解决了您的问题。我想这就是你要找的。我已经从您的原始代码中填充了许多缺失的 CSS,并使用了一些非 IE CSS 选择器来节省时间。我还重写了大部分js。肯定还有改进的空间,但效果很好。当窗口调整大小时,我也没有将其绑定为实时调整,但我认为与单击绑定相比,这更像是一种边缘情况。

You can use jQuery.fn.offset and compare to see if jQuery(window).width() is more than the sum of the left component of that offset and the width of your menu, and if it's not and the menu is cropped, then update the positioning of the menu to align itself with the other edge of the button (or menu trigger), etc. You'd bind this function to fire when the window gets resized or if there is horizontal scrolling. Sorry if this is too high-level and not enough example code, but it should be enough to get you started.

...

I've solved your problem with an example on JSFiddle. I think it's what you're looking for. I've filled in a lot of missing CSS from your original code, with some non-IE CSS selectors to save time. I also rewrote most of the js. There is definitely room for refinement, but it works well. I also didn't bind it to adjust live when the window is resizing, but I think that's more of an edge case compared to the click bindings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文