简体   繁体   English

鼠标移动动画

[英]Mousemove animation

Hello I am trying to achive this effect http://mario.ign.com/modern-era/super-mario-3D-world no mousemove I would like to do it with some kind of ease effect to make it smooth but actually dont know how to achive de desaceleration effect, so far what I've done is this http://jsfiddle.net/xtatanx/8RB24/1/ : 您好,我正在尝试达到这种效果http://mario.ign.com/modern-era/super-mario-3D-world no mousemove我想通过某种缓解效果来使其平滑,但实际上不知道如何达到解除脱皮的效果,到目前为止,我所做的是这个http://jsfiddle.net/xtatanx/8RB24/1/

var $container = $('#container');
var contWidth = $container.width();
var $point = $('.point');
var delay = 100;

$container.mousemove(function(e){
    clearTimeout(timer);

    var timer = setTimeout(function(){
        console.log(e.offsetX);
        $point.each(function(){
            if( e.offsetX > (contWidth - $point.width())){
                return;
            }
            var xp = $(this).position().left;
            xp += parseFloat((e.offsetX - xp)/ 20);
            $(this).css({
                left: xp
            });
        });
    }, delay);

});

But I think that the animation doesnt feel as smooth as mario site i would appreciate if you guys could help me ginding resources or guiding me to achieve this effect. 但我认为,如果您能帮助我获取资源或指导我实现这种效果,动画将不会像马里奥网站那样流畅。 Thank you very much. 非常感谢你。

Your choppiness is because it's running solely on the mousemove event. 您的断断续续是因为它仅在mousemove事件上运行。 If you break it out into an interval (say 30 frames per second), it will be much smoother. 如果将其分成一个间隔(例如每秒30帧),它将更加平滑。 This way it continues to ease even after the mouse stops. 这样,即使在鼠标停止后,它仍会继续缓解。

var $container = $('#container');
var contWidth = $container.width();
var $point = $('.point');
var delay = 100;
var decay = .1;

var intervalId;
var mouseX, mouseY;

//this function is called 30 times per second.
function ticker(){
     $point.each(function(){
         if( mouseX > (contWidth - $point.width())){
             mouseX = (contWidth - $point.width()); //instead of returning, just set the value to the max
         }
         var xp = $(this).position().left;
         xp += parseFloat((mouseX - xp) * decay); //using a decay variable for easier tweaking
         $(this).css({
            left: xp
         });
     });
}

//this interval calls the ticker function every 33 milliseconds
intervalId = setInterval(ticker, 33); //33 millisecond is about 30 fps  (16 would be roughly 60fps)

$container.mousemove(function(e){
    mouseX = e.offsetX; //store the current mouse position so we can reference it during the interval
    mouseY = e.offsetY;
});

take a look at this. 看看这个。 not sure it this is what you want but it sure does "a" trick. 不确定这是否是您想要的,但肯定会“欺骗”。

$container.mousemove(function(e){

    var xp = e.clientX-offset;
    blue.css({
        left: xp+'px'
     });

});

http://jsfiddle.net/d65yan/8RB24/2/ http://jsfiddle.net/d65yan/8RB24/2/

look at the .blue{} css some vendro prefix are needed if you want to support older versions of moz and chrome, forget about ie up to version 9 though 看看.blue {} css如果您想支持较旧的moz和chrome版本,则需要一些vendro前缀,尽管算了,直到第9版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM