/    Sign up×
Community /Pin to ProfileBookmark

Rotate picture with javascript

Hi,
I want to rotate a picture. Want to have 2 buttons, rotate left and right.

HTML-file
<br>
<img src=”../js/download.jpg” id=”img” alt=”diskett”>
<br>
<button class=”btn” id=”btnLeft”>Rotera vänster (Task 5)</button>
<button class=”btn” id=”btnRight”>Rotera höger (Task 5)</button>
<br><br>

Javascript
var img = document.getElementById(“img”)
var btnLeft = document.getElementById(“btnLeft”)
var btnRight = document.getElementById(“btnRight”)

let rotateL = 0
let rotateR = 0
btnLeft.addEventListener(“click”,()=>{
rotateL=rotateL+ -90
img.style.transform=’rotate(${rotateL}deg)’
});
btnRight.addEventListener(“click”,()=>{
rotateR=rotateR+ 90
img.style.transform=”rotate(${rotateR}deg)”
});

This does not work, do not understand.
Please helkp.

to post a comment
HTMLJavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Ashley128authorMar 21.2022 — Sempver any clue?
Copy linkTweet thisAlerts:
@Ashley128authorMar 21.2022 — Sempervivum?
Copy linkTweet thisAlerts:
@SempervivumMar 21.2022 — When you intend to insert variables into you string you have to use backticks:
img.style.transform=<span><code>rotate(${rotateL}deg)</code></span>
Copy linkTweet thisAlerts:
@Ashley128authorMar 21.2022 — Still no work
Copy linkTweet thisAlerts:
@SempervivumMar 21.2022 — I tested it now and after switching to backticks the code works for me.

Not sure if this meets your requirement but you could do with one variable for the rotation amount only. Doing so, the next rotation is always based on the last status even when the direction changes.
Copy linkTweet thisAlerts:
@Ashley128authorMar 21.2022 — It works now. I did a little mistake only
×

Success!

Help @Ashley128 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...