0
我是初學者,當談到純Javascript時。我想創建的效果像這樣使用jQuery創建:如何讓光標跟隨另一個div裏的光標,與純JS的mousemove
var mouseX = 0, mouseY = 0, limitX, limitY, containerWidth;
$(document).ready(function() {
//Declaring the container size variable when we dom is ready
//Grabbing the size of an element gives a number no longer a percentage
containerWidth = $(".container").width();
containerHeight = $(".container").height();
$("#debug").html('Container Width = ' + containerWidth + '<br/>Container Height = ' + containerHeight);
// cache the selector
var follower = $("#follower");
var xp = 0, yp = 0;
// limitX is now the difference between the #container's width (=80%) and 15px
limitX = containerWidth-15;
limitY = containerHeight-15;
var loop = setInterval(function(){
// change 12 to alter damping higher is slower
xp += (mouseX - xp)/6;
yp += (mouseY - yp)/6;
follower.css({left:xp, top:yp});
}, 15);
//Since changing the window size affects the width, we need to redefine the container size variable so that's it's current
$(window).resize(function() {
//this makes limiX change based on container width
limitX = $(".container").width()-15;
$("#debug").html('Container Width = ' + containerWidth + '<br/>Container Height = ' + containerHeight);
}).resize();
$(document).on('mousemove', function (e) {
mouseX = Math.min(e.pageX, limitX);
mouseY = Math.min(e.pageY, limitY);
});
});
鏈接到用戶的小提琴:http://jsfiddle.net/alexchopjian/5QfYL/5/, 但使用純JavaScript。可能嗎?
我將非常感謝任何線索如何得到這個。
你好,我的意思是作出書面jQuery中成純JavaScript的解決方案上面的代碼。 – Ndeesmooth
查看上一個問題:[純JavaScript的可拖動元素](http://stackoverflow.com/questions/29703473/pure-javascript-draggable-element) – Roberto
這是可能的,因爲jQuery只是更多的JavaScript。見羅伯託的評論。我明白不希望圖書館太高興,但考慮到它是一個經過高度測試的庫,可以簡化瀏覽器的兼容性,使用jQuery確實是一個小的代價,它使得js必須保持更小,更易於編寫和維護。 –