2016-02-09 80 views
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。可能嗎?

我將非常感謝任何線索如何得到這個。

+0

你好,我的意思是作出書面jQuery中成純JavaScript的解決方案上面的代碼。 – Ndeesmooth

+1

查看上一個問題:[純JavaScript的可拖動元素](http://stackoverflow.com/questions/29703473/pure-javascript-draggable-element) – Roberto

+0

這是可能的,因爲jQuery只是更多的JavaScript。見羅伯託的評論。我明白不希望圖書館太高興,但考慮到它是一個經過高度測試的庫,可以簡化瀏覽器的兼容性,使用jQuery確實是一個小的代價,它使得js必須保持更小,更易於編寫和維護。 –

回答

0

這不是完全相同的副本,但它是一個起點:

var mouseX = 0, mouseY = 0, limitX, limitY, containerWidth; 
 

 
window.onload = function(e) { 
 
    var containerObjStyle = window.getComputedStyle(document.querySelectorAll(".container")[0]); 
 
    containerWidth = parseFloat(containerObjStyle.width).toFixed(0); 
 
    containerHeight = parseFloat(containerObjStyle.height).toFixed(0); 
 
    document.getElementById('debug').innerHTML = 'Container Width = ' + containerWidth + '<br/>Container Height = ' + containerHeight; 
 
    var follower = document.getElementById("follower"); 
 
    var xp = 0, yp = 0; 
 
    limitX = containerWidth-15; 
 
    limitY = containerHeight-15; 
 
    var loop = setInterval(function(){ 
 
    xp = (mouseX == limitX) ? limitX : mouseX -7; 
 
    xp = (xp < 0) ? 0 : xp; 
 
    yp = (mouseY == limitY) ? limitY : mouseY -7; 
 
    yp = (yp < 0) ? 0 : yp; 
 
    follower.style.left = xp + 'px'; 
 
    follower.style.top = yp + 'px'; 
 
    }, 15); 
 
    window.onresize = function(e) { 
 
    limitX = parseFloat(window.getComputedStyle(document.querySelectorAll(".container")[0]).width).toFixed(0); 
 
    document.getElementById("debug")[0].innerHTML='Container Width = ' + containerWidth + '<br/>Container Height = ' + containerHeight; 
 
    } 
 
    document.onmousemove = function(e) { 
 
    mouseX = Math.min(e.pageX, limitX); 
 
    mouseY = Math.min(e.pageY, limitY); 
 
    } 
 
};
#follower{ 
 
    position : relative; 
 
    background-color : yellow; 
 
    width:15px; 
 
    height:15px; 
 
    border-radius:50px; 
 
} 
 

 

 
.container { 
 
    width:80%; 
 
    height:150px; 
 
    position:absolute; 
 
    top:0; 
 
    left:0; 
 
    overflow:hidden; 
 
    border:1px solid #000000; 
 
}
<p id="debug"></p> 
 
<div class="container"> 
 
    <div id="follower"></div> 
 
</div>

+0

謝謝你的迴應,那正是我一直在尋找的!你可能會寫如何將光標居中放在光標上嗎?再次感謝! – Ndeesmooth