TJeffer TJeffer - 1 month ago 12
HTML Question

Jsfiddle won't work when copying into Dreamweaver

I've been trying to solve this by myself, and I just can't figure out what I'm doing wrong. But this jsfiddle: http://jsfiddle.net/3964w/3/ completely stops working when I try to work with it in Dreamweaver.

Here is my html code, if that helps. I'm feeling so lost on this, and I think it should be pretty easy. If someone could explain what I'm doing wrong I would be very grateful.

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>testing jquery</title>
<link href="styles.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script>

var mouseX = 0, mouseY = 0, limitX = 150-15, limitY = 150-15;
$(window).mousemove(function(e){
var offset = $('.container').offset();
mouseX = Math.min(e.pageX - offset.left, limitX);
mouseY = Math.min(e.pageY - offset.top, limitY);
if (mouseX < 0) mouseX = 0;
if (mouseY < 0) mouseY = 0;
});

// cache the selector
var follower = $("#follower");
var xp = 0, yp = 0;
var loop = setInterval(function(){
// change 12 to alter damping higher is slower
xp += (mouseX - xp) / 12;
yp += (mouseY - yp) / 12;
follower.css({left:xp, top:yp});

}, 30);

</script>

</head>

<body>

<div class="centerdiv">
<div class="container">
<div id="follower"></div>
</div>
</div>

</body>

</html>

Answer

Are you calling jQuery from a CDN or a local directory? If from a CDN it won't work unless the page is being run from a server, like localhost. So if you don't have a local server set up then download a copy of jQuery and put it inside the directory where you are putting JavaScript files and call it from there.

Or USe this

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

or write your code inside document ready function

<script type="text/javascript"> 
$(document).ready(function()
{ /* Your Code Here */ });
 </script>
Comments