-1

need some help.. i want the bin to hide on load and i have a draggable div, so when i drag the div the bin should show and when i leave the div the bin should hide again?

<html>
<head>
  <style>
  .box{
    border: 1px solid black;
  }
  </style>

  <script>
  window.onload = function() { document.getElementById('myDIV').style.display = 'none'; };
  function showImg(){
    var x = document.getElementById('myDIV');
    if (x.style.display === 'none') { x.style.display = 'block'; }
    else { x.style.display = 'none'; }
  }
  </script>

</head>

<body>
  <div id="show" onclick="showImg()" class="box">
    when this div is select and drag the bin should be shown
  </div>

  <div id="myDIV">
    <img src="img/upload.png"/>
  </div>

</body>
</html>
11
  • What is the problem u r facing? Commented Jun 6, 2017 at 2:40
  • That is a cute unclear... Commented Jun 6, 2017 at 2:41
  • What is «the bin to hide» ? Commented Jun 6, 2017 at 2:42
  • 1
    the bin is the image? Commented Jun 6, 2017 at 2:44
  • 1
    i think you need to use ondrag event Commented Jun 6, 2017 at 2:46

4 Answers 4

1

you can use this :

            // for show element when start drag    
            document.addEventListener("dragstart", function (event) {
                if (event.target.className == "box") {
                    var x = document.getElementById('myDIV');
                    x.style.display = 'block';
                }
            });
       // for hide element when drag end
        document.addEventListener("dragend", function (event) {
            if (event.target.className == "box") {
                var x = document.getElementById('myDIV');
                x.style.display = 'none';
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

Using jQuery UI, you can catch drag events easily in this way:

  $("#myDIV").draggable({
     start: function() {

     },
     drag: function() {

     },
     stop: function() {

     }
  });

Comments

0

I think you need ondrag event instead of click event.

   ondrag="function()"
   ondrop="function()"
   ondragover="function()"
   ondragstart="function()" 

Comments

0

This will work.

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <style>
        .box {
            border: 1px solid black;
        }
    </style>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        window.onload = function () { document.getElementById('myDIV').style.display = 'none'; };
        function showImg() {
            var x = document.getElementById('myDIV');
            if (x.style.display === 'none') {
                x.style.display = 'block';
            }
            else { x.style.display = 'none'; }
        }

        $(function () {
            $("#show").draggable();
        });
    </script>

</head>

<body>
    <div id="show" onclick="showImg()" class="box">
        when this div is select and drag the bin should be shown
    </div>

    <div id="myDIV">
        <img src="~/Images/untitled.gif" />
        @*<img src="img/upload.png" />*@
    </div>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.