How to load next draggable item only after the current item has been moved?












0















I want multiple draggable items to "come out" of the same place. When the first one is dragged away, the second one loads and becomes available to drag. When the second one is dragged away, the third div will load, and so on.



Right now, Div 2 is already loaded and in a visible state regardless of where Div 1 is. When you move Div 1 away, you'll see the underlying Div 2. What I want is for Div 2 to be "hidden". It should only load and become visible when Div 1 is dragged away.



Thanks for your help.



What I've tried.



https://jsfiddle.net/0wg8sqmz/



<div id="drag1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>
<div id="drag2" class="drag" style="left:10px;top:-90px;background-color:lightblue">Draggable 2</div>


JS



$(function () {
$(".drag").draggable({
stack: ".drag"
});
});









share|improve this question



























    0















    I want multiple draggable items to "come out" of the same place. When the first one is dragged away, the second one loads and becomes available to drag. When the second one is dragged away, the third div will load, and so on.



    Right now, Div 2 is already loaded and in a visible state regardless of where Div 1 is. When you move Div 1 away, you'll see the underlying Div 2. What I want is for Div 2 to be "hidden". It should only load and become visible when Div 1 is dragged away.



    Thanks for your help.



    What I've tried.



    https://jsfiddle.net/0wg8sqmz/



    <div id="drag1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>
    <div id="drag2" class="drag" style="left:10px;top:-90px;background-color:lightblue">Draggable 2</div>


    JS



    $(function () {
    $(".drag").draggable({
    stack: ".drag"
    });
    });









    share|improve this question

























      0












      0








      0








      I want multiple draggable items to "come out" of the same place. When the first one is dragged away, the second one loads and becomes available to drag. When the second one is dragged away, the third div will load, and so on.



      Right now, Div 2 is already loaded and in a visible state regardless of where Div 1 is. When you move Div 1 away, you'll see the underlying Div 2. What I want is for Div 2 to be "hidden". It should only load and become visible when Div 1 is dragged away.



      Thanks for your help.



      What I've tried.



      https://jsfiddle.net/0wg8sqmz/



      <div id="drag1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>
      <div id="drag2" class="drag" style="left:10px;top:-90px;background-color:lightblue">Draggable 2</div>


      JS



      $(function () {
      $(".drag").draggable({
      stack: ".drag"
      });
      });









      share|improve this question














      I want multiple draggable items to "come out" of the same place. When the first one is dragged away, the second one loads and becomes available to drag. When the second one is dragged away, the third div will load, and so on.



      Right now, Div 2 is already loaded and in a visible state regardless of where Div 1 is. When you move Div 1 away, you'll see the underlying Div 2. What I want is for Div 2 to be "hidden". It should only load and become visible when Div 1 is dragged away.



      Thanks for your help.



      What I've tried.



      https://jsfiddle.net/0wg8sqmz/



      <div id="drag1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>
      <div id="drag2" class="drag" style="left:10px;top:-90px;background-color:lightblue">Draggable 2</div>


      JS



      $(function () {
      $(".drag").draggable({
      stack: ".drag"
      });
      });






      javascript jquery jquery-ui-draggable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 11:42









      JoshuaJoshua

      4317




      4317
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You could do that quite easily by using data attributes.



          See the below code.



          Each DIV has a data-index attribute (for example data-index="1") and when an element is dragged (drag has stopped), it will show the element with the next index let next = $(this).data('index') + 1;.



          Every DIV except the first one is set to display: none to hide them in the first place.






          $(function() {
          $(".drag").draggable({
          stack: ".drag",
          stop: function() {

          // Calculate next index
          let next = $(this).data('index') + 1;

          // Show next DIV based on data-index
          $('div[data-index="' + next + '"]').show();
          }
          });
          });

          .drag {
          width: 100px;
          height: 100px;
          border: 1px solid black;
          cursor: pointer;
          border-radius: 10px;
          text-align: center;
          background-color: lightpink;
          position: absolute;
          }

          <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

          <!-- HTML -->
          <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

          <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

          <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>








          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446048%2fhow-to-load-next-draggable-item-only-after-the-current-item-has-been-moved%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You could do that quite easily by using data attributes.



            See the below code.



            Each DIV has a data-index attribute (for example data-index="1") and when an element is dragged (drag has stopped), it will show the element with the next index let next = $(this).data('index') + 1;.



            Every DIV except the first one is set to display: none to hide them in the first place.






            $(function() {
            $(".drag").draggable({
            stack: ".drag",
            stop: function() {

            // Calculate next index
            let next = $(this).data('index') + 1;

            // Show next DIV based on data-index
            $('div[data-index="' + next + '"]').show();
            }
            });
            });

            .drag {
            width: 100px;
            height: 100px;
            border: 1px solid black;
            cursor: pointer;
            border-radius: 10px;
            text-align: center;
            background-color: lightpink;
            position: absolute;
            }

            <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

            <!-- HTML -->
            <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

            <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

            <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>








            share|improve this answer




























              0














              You could do that quite easily by using data attributes.



              See the below code.



              Each DIV has a data-index attribute (for example data-index="1") and when an element is dragged (drag has stopped), it will show the element with the next index let next = $(this).data('index') + 1;.



              Every DIV except the first one is set to display: none to hide them in the first place.






              $(function() {
              $(".drag").draggable({
              stack: ".drag",
              stop: function() {

              // Calculate next index
              let next = $(this).data('index') + 1;

              // Show next DIV based on data-index
              $('div[data-index="' + next + '"]').show();
              }
              });
              });

              .drag {
              width: 100px;
              height: 100px;
              border: 1px solid black;
              cursor: pointer;
              border-radius: 10px;
              text-align: center;
              background-color: lightpink;
              position: absolute;
              }

              <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

              <!-- HTML -->
              <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

              <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

              <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>








              share|improve this answer


























                0












                0








                0







                You could do that quite easily by using data attributes.



                See the below code.



                Each DIV has a data-index attribute (for example data-index="1") and when an element is dragged (drag has stopped), it will show the element with the next index let next = $(this).data('index') + 1;.



                Every DIV except the first one is set to display: none to hide them in the first place.






                $(function() {
                $(".drag").draggable({
                stack: ".drag",
                stop: function() {

                // Calculate next index
                let next = $(this).data('index') + 1;

                // Show next DIV based on data-index
                $('div[data-index="' + next + '"]').show();
                }
                });
                });

                .drag {
                width: 100px;
                height: 100px;
                border: 1px solid black;
                cursor: pointer;
                border-radius: 10px;
                text-align: center;
                background-color: lightpink;
                position: absolute;
                }

                <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

                <!-- HTML -->
                <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

                <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

                <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>








                share|improve this answer













                You could do that quite easily by using data attributes.



                See the below code.



                Each DIV has a data-index attribute (for example data-index="1") and when an element is dragged (drag has stopped), it will show the element with the next index let next = $(this).data('index') + 1;.



                Every DIV except the first one is set to display: none to hide them in the first place.






                $(function() {
                $(".drag").draggable({
                stack: ".drag",
                stop: function() {

                // Calculate next index
                let next = $(this).data('index') + 1;

                // Show next DIV based on data-index
                $('div[data-index="' + next + '"]').show();
                }
                });
                });

                .drag {
                width: 100px;
                height: 100px;
                border: 1px solid black;
                cursor: pointer;
                border-radius: 10px;
                text-align: center;
                background-color: lightpink;
                position: absolute;
                }

                <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

                <!-- HTML -->
                <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

                <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

                <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>








                $(function() {
                $(".drag").draggable({
                stack: ".drag",
                stop: function() {

                // Calculate next index
                let next = $(this).data('index') + 1;

                // Show next DIV based on data-index
                $('div[data-index="' + next + '"]').show();
                }
                });
                });

                .drag {
                width: 100px;
                height: 100px;
                border: 1px solid black;
                cursor: pointer;
                border-radius: 10px;
                text-align: center;
                background-color: lightpink;
                position: absolute;
                }

                <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

                <!-- HTML -->
                <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

                <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

                <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>





                $(function() {
                $(".drag").draggable({
                stack: ".drag",
                stop: function() {

                // Calculate next index
                let next = $(this).data('index') + 1;

                // Show next DIV based on data-index
                $('div[data-index="' + next + '"]').show();
                }
                });
                });

                .drag {
                width: 100px;
                height: 100px;
                border: 1px solid black;
                cursor: pointer;
                border-radius: 10px;
                text-align: center;
                background-color: lightpink;
                position: absolute;
                }

                <link href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" rel="stylesheet"/>

                <!-- HTML -->
                <div id="drag1" data-index="1" class="drag" style="left:10px;top:20px;background-color:orange">Draggable 1</div>

                <div id="drag2" data-index="2" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 2</div>

                <div id="drag3" data-index="3" class="drag" style="left:10px;top:20px;background-color:lightblue; display: none;">Draggable 3</div>

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 12:25









                MrUpsidownMrUpsidown

                15.3k85096




                15.3k85096
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446048%2fhow-to-load-next-draggable-item-only-after-the-current-item-has-been-moved%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Xamarin.form Move up view when keyboard appear

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Anylogic : not able to use stopDelay()