拖动 Div 上的 jQuery 盲效果

发布于 2024-11-08 05:35:15 字数 4419 浏览 0 评论 0原文

我在页面上有许多可以拖放的 div。我还对它们实现了盲化效果,这样如果我不想看到它,我可以最小化和最大化内容。

我遇到一个问题,如果我有 3 个项目垂直堆叠在一起,然后我将底部的一个移动到顶部的右侧并最小化顶部的 div,所有内容都会向上滑动 - 而我的第三个 div幻灯片直接从屏幕上移开!

我尝试过很多东西,比如让 div 使用绝对定位,但这会导致 div 在某些情况下无法向上滑动的问题。由于偏移和相对定位,动态重新排序 div 会导致 div 在屏幕上四处乱窜。

我只是想要这样,当用户将 div 拖动到左侧或右侧并且“较早”的 div 最小化时,所有后续的 div 都不会移动。

对此的任何建议都将不胜感激。

编辑1: 我在绝对定位方面遇到的问题如下。 我从垂直列中的 A、B 和 C 开始。所有项目均已展开。我将 B 移到 A 的右侧,将 C 移到 B 的正下方。这给了我 2 列(A 是一列,B、C 是另一列)。一切都是绝对的,如果我尝试关闭 B,那么 C 就不会向上移动 - 这是理所当然的。 然后我尝试“选择性地”将事物设为绝对,从而在相对和绝对之间切换,但我遇到了坐标问题。如果您有相对位置且 left:100px 和 top:-50px,则将位置翻转为绝对位置会导致在绝对上下文中解释这些坐标。我的控制飞出了屏幕。我尝试通过使用 jQuery 的偏移函数获取绝对坐标来解决这个问题,但这会返回相对坐标,我被卡住了。我尝试自己维护绝对坐标,但由于某种原因它也不起作用。它已经失去控制了:)。

Javascript

加载页面时会调用此 javascript 绑定。我将此函数绑定到 PNG 箭头,这样当按下箭头时,相应 div 中的内容就会展开和收缩。

                $('.ArrowMargin').bind('click', function () {

                var splits = this.src.split("/");

                var action = "";
                if (splits.length >= 2) {
                    var folder = splits[splits.length - 2];
                    var image = splits[splits.length - 1];

                    if (folder == "Images") {
                        if (image == "arrow_open.png") {
                            action = "close";
                            this.src = "Images/arrow_closed.png";
                        } else {
                            action = "open";
                            this.src = "Images/arrow_open.png";
                        }
                    }
                }
                var divs = document.getElementsByTagName("div");

                if (action != "") {
                    var options = {};
                    for (var i = 0; i < divs.length; i++) {
                        var element = divs[i];
                        if (element.className.indexOf("Hideable") != -1) {
                            if (this.parentNode.parentNode == element.parentNode) {
                                if (action == "open") {
                                    var jQueryObj = jQuery(element);
                                    jQueryObj.show("blind", options, 500, null);
                                } else {
                                    var jQueryObj = jQuery(element);
                                    jQueryObj.hide("blind", options, 500, null);
                                }
                                break;
                            }

                        }
                    }

CSS

这是我设置一些简单样式的 CSS 内容。我有一些空样式,用于根据类访问 div。

            .ArrowMargin { float:right; margin:0 5px 0 0; }         

            .alpha { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px;  }

        .bravo { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px; }

        .delta { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px;}

        .charlie{}

        .echo{}

HTML BODY

这是正文。它只是一些代表不同内容的 div。标有“Hideable”类的 div 是 jQuery 盲的。

<body>
    <div class="alpha">
        <div class="LeftColumnCellTitle">                
            <span class="TitleMargin">foobar1</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>            
        <div class="ui-widget-content ui-corner-all Hideable Center"></div>
    </div>

    <div class="bravo">
        <div class="LeftColumnCellTitle">
            <span class="TitleMargin">foobar2</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>
        <div class="ui-widget-content ui-corner-all Hideable charlie"></div>
    </div>

    <div class="delta">
        <div class="LeftColumnCellTitle">
            <span class="TitleMargin">foobar3</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>
        <div class="ui-widget-content ui-corner-all Hideable echo"></div>
    </div>
</body>

I've got a number of divs on a page that I can drag and drop around. I also implemented the blind effect on them so that I can minimize and maximize the content if i don't want to see it.

I've got a problem that if I have 3 items stacked on top of each other, vertically, and I move the bottom one to the right of the top one and minimize the top div, everything slides upwards - and the 3rd div that I moved up slides right off the screen!

I've tried a ton of stuff, like making divs use absolute positioning but that causes problems of divs not sliding upwards in some circumstances. Reordering the divs dynamically causes the divs to be thrown around the screen because of offsets and relative positioning.

I just want it so that when the user drags divs over to the left or right and an "earlier" div is minimized, all subsequent divs don't get moved.

Any suggestions on this one are greatly appreciated.

Edit 1:
The problem I'm having with the absolute positioning is as follows.
I start with A, B, and C in a vertical column. All items are expanded. I move B to the right side of A and C right under B. This gives me 2 columns (A being one and B,C being the other). With everything being absolute, if I try to close B, then C doesn't move up - rightfully so.
I tried then making things "selectively" absolute, thereby flipping between relative and absolute but I got into a problem with coordinates. If you have a relative position and left:100px and top:-50px, then flipping the position to absolute causes these coordinates to be interpreted within an absolute context. My control flies off the screen. I tried fixing this by getting the absolute coordinates using jQuery's offset function, however this returns the relative coordinates and I'm stuck. I tried to maintain the absolute coordinates myself, but it didn't work either for some reason. It's getting out of control :).

Javascript

This javascript bind is called when the page is loaded. I bind this function to a PNG arrow so that when the arrow is pressed, the content in the appropriate div expands and contracts.

                $('.ArrowMargin').bind('click', function () {

                var splits = this.src.split("/");

                var action = "";
                if (splits.length >= 2) {
                    var folder = splits[splits.length - 2];
                    var image = splits[splits.length - 1];

                    if (folder == "Images") {
                        if (image == "arrow_open.png") {
                            action = "close";
                            this.src = "Images/arrow_closed.png";
                        } else {
                            action = "open";
                            this.src = "Images/arrow_open.png";
                        }
                    }
                }
                var divs = document.getElementsByTagName("div");

                if (action != "") {
                    var options = {};
                    for (var i = 0; i < divs.length; i++) {
                        var element = divs[i];
                        if (element.className.indexOf("Hideable") != -1) {
                            if (this.parentNode.parentNode == element.parentNode) {
                                if (action == "open") {
                                    var jQueryObj = jQuery(element);
                                    jQueryObj.show("blind", options, 500, null);
                                } else {
                                    var jQueryObj = jQuery(element);
                                    jQueryObj.hide("blind", options, 500, null);
                                }
                                break;
                            }

                        }
                    }

CSS

This is the CSS stuff where I set up some simple styles. I have some empty styles that I use to access divs based on class.

            .ArrowMargin { float:right; margin:0 5px 0 0; }         

            .alpha { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px;  }

        .bravo { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px; }

        .delta { width:300px; background-color:#000000; border-color:#424242; border-width:1px; border-style:solid; color:#c59e32; -moz-border-radius: 15px;}

        .charlie{}

        .echo{}

HTML BODY

This is the body. It's just a number of divs that represent different pieces of content. The divs marked with the "Hideable" class are those that are jQuery blinded.

<body>
    <div class="alpha">
        <div class="LeftColumnCellTitle">                
            <span class="TitleMargin">foobar1</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>            
        <div class="ui-widget-content ui-corner-all Hideable Center"></div>
    </div>

    <div class="bravo">
        <div class="LeftColumnCellTitle">
            <span class="TitleMargin">foobar2</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>
        <div class="ui-widget-content ui-corner-all Hideable charlie"></div>
    </div>

    <div class="delta">
        <div class="LeftColumnCellTitle">
            <span class="TitleMargin">foobar3</span>
            <img class="ArrowMargin" src="Images/arrow_open.png" alt="Open"/>
        </div>
        <div class="ui-widget-content ui-corner-all Hideable echo"></div>
    </div>
</body>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文