</head>
<body>
<script>
var temp;
document.addEventListener("click",(function(e) {
temp=document.createElement("img")
temp.src="https://images.freeimages.com/images/large-previews/82d/eastern-rosella-1-1617233.jpg"
temp.style+="left:0; top:0;"
temp.style.position+="absolute"
temp.height="200"
temp.width="200"
temp.style.left=e.clientX
temp.style.top=e.clientY
temp.style.transition="all 2s"
document.body.append(temp)
temp.style.transform="translate(0px, 50px)"
}))
</script>
</body>
This piece of code is meant to create an image at the user’s mouse click, that will move it down by 50px
, with a transition duration of 2s. However, it seems to ignore the transition property, and teleport down 50px
instead without the animation.
I have also added ease 2s
to see if the translation of 50px would be delayed by 2 secs, but it ignored that.
I have also tried using transform+=
instead of transform=
, if that would do anything, but it didn’t.
This is the created element:
<img src="https://images.freeimages.com/images/large-previews/82d/eastern-rosella-1-1617233.jpg" height="200" width="200" style="top: 316px; position: absolute; left: 849px; transition: all 2s; transform: translate(0px, 50px);">
The transition property is as it should be, but it has no effect.
How do I make the image element move smoothly, instead of changing position?
2
Answers
Add an
img.style.transform
to theimg.onload
event:This code give you ability to move the image independently to where you click.
If you want it to always move to 50px below, then just change the js to this code below.