Javascript 在悬停时给图像显示的移动增加一点滞后/弹性
我在一些文本上设置了图像显示效果。目前,当你四处移动时,图像会粘在鼠标光标上,但我需要它有一点滞后/弹性,这样它感觉更自然,理想情况下就像codepen的图像一样 我一直在绞尽脑汁想办法让它发挥作用,所以感谢任何帮助Javascript 在悬停时给图像显示的移动增加一点滞后/弹性,javascript,jquery,css,Javascript,Jquery,Css,我在一些文本上设置了图像显示效果。目前,当你四处移动时,图像会粘在鼠标光标上,但我需要它有一点滞后/弹性,这样它感觉更自然,理想情况下就像codepen的图像一样 我一直在绞尽脑汁想办法让它发挥作用,所以感谢任何帮助 函数_instanceof(left,right){if(right!=null&&typeof Symbol!=“undefined”&&right[Symbol.hasInstance]){return!!right[Symbol.hasInstance](left);}否则
函数_instanceof(left,right){if(right!=null&&typeof Symbol!=“undefined”&&right[Symbol.hasInstance]){return!!right[Symbol.hasInstance](left);}否则{return left instanceof right;}
函数toConsumableArray(arr){返回ArrayWithout Holes(arr)| | | | | | | | | | | | | | | | | | | | |
函数_nonIterableSpread(){抛出新的TypeError(“传播不可iterable实例的尝试无效。\n要使其可iterable,非数组对象必须具有[Symbol.iterator]()方法。”);}
函数_unsupportedeterabletoarray(o,minLen){if(!o)return;if(typeof o==“string”)return _arraylikotarray(o,minLen);var n=Object.prototype.toString.call(o).slice(8,-1);if(n==“Object”&&o.constructor)n=o.constructor.name;if(n==“Map”| | n==“Set”)返回数组。from(o);if(n==“Ui=”Arguments”=“Ui=”Arguments”/(1248(?)(?:钳制)?数组$/.test(n))返回_arraylikotearray(o,minLen);}
函数_iterableToArray(iter){if(typeof Symbol!=“未定义”&&Symbol.iterator in Object(iter))返回数组。from(iter);}
函数_arraywhithoutholes(arr){if(Array.isArray(arr))返回_arrayLikeToArray(arr);}
函数_arraylikotearray(arr,len){if(len==null | | len>arr.length)len=arr.length;for(var i=0,arr2=new Array(len);i 对于(var i=0;i,因为您使用的是TweenMax,所以您可以使用TweenMax缓解功能。事实上,您已经这样做了,当前图像在动画结束时会减慢一点。这称为easeOut
。您可以在代码笔的第135行看到easeOut
效果:
this.tl.add(new TweenMax(this.DOM.revealImgs[i], i === this.totalImages - 1 ? 1.2 : 0.55, {
ease: i === this.totalImages - 1 ? Quint.easeOut : Quad.easeOut,
...etc...
这意味着,如果i
等于totalImages-1
,则松弛为Quint.easeOut
,否则为Quad.easeOut
你可以随意改变这些放松方式!如果你想要更具弹性的感觉,你可以试试
ease:Back.easeOut
您可以在此处找到所有可能的缓解功能:
更新
我添加了一个用于在图像位置之间切换的功能,而不仅仅是在鼠标移动时设置X和Y位置。现在您可以使用与上面相同的缓和功能:
// new function tweenElement
this.tweenElement = function (ev) {
var mousePos = getMousePos(ev);
var docScrolls = {
left: document.body.scrollLeft + document.documentElement.scrollLeft,
top: document.body.scrollTop + document.documentElement.scrollTop
};
// here you can add easing functions to tweenmax
TweenMax.to(_this.DOM.reveal, 1, {"top":mousePos.y + 20 - docScrolls.top, "left":mousePos.x + 20 - docScrolls.left, ease:Quad.easeOut})
};
// this function is changed and now calls tweenElement instead of positionElement when the mouse moves
this.mousemoveFn = function (ev) {
return requestAnimationFrame(function () {
_this.tweenElement(ev);
});
};
codepen谢谢你的回复,但这是图像入口动画,我想改变的是当你移动鼠标时图像的移动方式为什么不使用TweenMax呢?顺便问一下,这是生成的代码吗?它似乎不是手工编码的。是的,从这里开始-我不知道如何使用TweenMax来改变I的移动mage,这就是为什么我在这里问你可能会更改mousemovfn()
函数,并在其中添加tween。我已经用一个例子更新了我的答案。但这有点棘手,因为移动鼠标时会添加很多tween。不过它似乎起作用:)