I seem to be having issues in Dreamweaver.
Is it me, or is this code not compatible somehow in Dreamweaver?
probably me most likely.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="veiwport" content="width=device-width, initial-scale=1.0">
<title>Cursor Follower</title>
<style>
#customCursor {
--cursorXPos: 0;
--cursorYPos: 0;
position: fixed;
top: -25px;
left: -25px;
transform: translated3d(var(--cursorXPos), var(--cursorYPos), 0);
transition: tranform 2s cubic-bezier(0.75, 0.885, 0.32, 1.275);
width: 50px
}
</style>
</head>
<body>
<div id="customCursor">
<img src="UFO.png" alt="customCursor">
</div>
<script>
let mouseX = 0
let mouseY = 0
let customCursor = document.querySelector("#customCursor");
document.addEventListener("mousemove", setMousePosition, false);
function moveCursor(timestamp){
customCursor.style.setProperty("--cursorXPos",mouseX + "px");
customCursor.style.setProperty("--cursorYPos",mouseY + "px");
requestAnimationFrame(moveCursor);
}
function setMousePosition(event){
mouseX = event.clientX;
mouseY = event.clientY;
}
</script>
<style>
</style>
</body>
</html>
When you run it in the browser, what happens? Dreamweaver is your code editor, and interactive content typically will not work inside it.
The png of a UFO is the only thing that shows up, not even when I downsized it to 50 px.
This is unusual, I have done something similar to this before. For some reason while following the tutorial nothing happens.
That is expected unfortunately. You should use your browser as your source of truth for what your page looks like. Dreamweaver’s preview engine doesn’t understand a lot of modern techniques like CSS custom properties, for example.
Using Dreamweaver as a code editor is fine, but you shouldn’t rely on it to give you an accurate preview experience.
Well, it’s not just that. Whenever I preview the code on my browser, in my case edge, I still see the same thing. I’m convinced I’m missing something in my code, but I’m not sure.
Does your browser’s console show any errors?
I see what is going on here! You were very close
In your CSS, it is translate3d (not translated3d ):
transform: translated3d(var(--cursorXPos), var(--cursorYPos), 0);
Next, you have your timer loop defined in moveCursor
, but you never actually call it to kick it off. Add a call to moveCursor()
, and that will get the party started!
Here is my version that works:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="veiwport" content="width=device-width, initial-scale=1.0">
<title>Cursor Follower</title>
<style>
#customCursor {
--cursorXPos: 0;
--cursorYPos: 0;
position: fixed;
top: -25px;
left: -25px;
transform: translate3d(var(--cursorXPos), var(--cursorYPos), 0);
transition: tranform 2s cubic-bezier(0.75, 0.885, 0.32, 1.275);
width: 50px
}
img {
width: 100px;
height: 100px;
background-color: black;
}
</style>
</head>
<body>
<div id="customCursor">
<img alt="customCursor">
</div>
<script>
let mouseX = 0
let mouseY = 0
let customCursor = document.querySelector("#customCursor");
document.addEventListener("mousemove", setMousePosition, false);
function moveCursor(timestamp) {
customCursor.style.setProperty("--cursorXPos", mouseX + "px");
customCursor.style.setProperty("--cursorYPos", mouseY + "px");
console.log("Code called! " + mouseX + " " + mouseY);
requestAnimationFrame(moveCursor);
}
function setMousePosition(event) {
mouseX = event.clientX;
mouseY = event.clientY;
//console.log("Mouse moving!");
}
moveCursor();
</script>
<style>
</style>
</body>
</html>
Its working!!
Although I noticed that it doesn’t have that smooth lag transition. Is that another Dreamweaver issue?
kirupa:
transition: tranform 2s
Another small misspelling It should be transform .
Ah I see, it’s works perfectly!
Thank you!
1 Like
kirupa
May 10, 2023, 8:20pm
12
Glad it works! I wish there were ways of catching errors in CSS in the same way we can for JavaScript.
Same, would definitely make the job easier!
1 Like