'How do I move an element diagonally and after another animation in jQuery?
I have an HTML/CSS element:
<div id="box">
<p> BOX </p>
</div>
#box {
width: 100px;
height: 200px;
background-color: skyblue;
position: absolute;
left: 0px;
top: 200px;
text-align: center;
font-family: Times New Roman;
}
I need it to move from its position in the middle of the page diagonally down and to the left (to the left corner). I've tried this:
$(function() {
$("button").click(function() {
$("#box").animate({
left: $(window).width() - 200
bottom: $(window).width() - 200
}
});
});
How do I do this with jQuery and have it done after it's been on the page a few seconds (for a different animation to occur first)?
Solution 1:[1]
Consider the following.
$(function() {
$("button").click(function() {
$("#box").animate({
left: $(window).width() - 100,
top: $(window).height() - 200
});
});
});
#box {
width: 100px;
height: 200px;
background-color: skyblue;
position: absolute;
left: 0px;
top: 40px;
text-align: center;
font-family: Times New Roman;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="box">
<p>BOX</p>
</div>
<button>Go</button>
You were using width property for both. You must use width and height. Also it's better to use top versus bottom in this case. You can use bottom, yet I would suggest something like:
bottom: $("#box").height()
In this way, the property of the top will be N Pixels from the "Bottom".
See More: https://www.w3schools.com/cssref/pr_pos_bottom.asp
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Twisty |
