【发布时间】:2020-08-27 13:47:36
【问题描述】:
有没有办法实现如下网站的鼠标悬停效果:
https://studiomaertens.com/about
如果您将鼠标悬停在“工作”或“关于”链接上,则会对其产生磁效应,并且光标会自动缩放并将其自身定位在链接的中心,
到目前为止,我已经设法将两个不同的示例组合在一起,磁效应有效,但光标与图标没有正确对齐
https://codepen.io/pen/?template=mdPmPbK
<main>
<div>
<button class="cerchio" >
<ion-icon name="logo-facebook"></ion-icon>
</button>
</div>
<button>
<ion-icon name="logo-twitter"></ion-icon>
</button>
</main>
<div class="cursor cursor--large"></div>
<div class="cursor cursor--small"></div>
var cerchio = document.querySelectorAll('.cerchio');
cerchio.forEach(function(elem){
$(document).on('mousemove touch', function(e){
magnetize(elem, e);
});
})
function magnetize(el, e){
var mX = e.pageX,
mY = e.pageY;
const item = $(el);
const customDist = item.data('dist') * 20 || 120;
const centerX = item.offset().left + (item.width()/2);
const centerY = item.offset().top + (item.height()/2);
var deltaX = Math.floor((centerX - mX)) * -0.45;
var deltaY = Math.floor((centerY - mY)) * -0.45;
var distance = calculateDistance(item, mX, mY);
if(distance < customDist){
TweenMax.to(item, 0.5, {y: deltaY, x: deltaX, scale:1.1});
item.addClass('magnet');
}
else {
TweenMax.to(item, 0.6, {y: 0, x: 0, scale:1});
item.removeClass('magnet');
}
}
function calculateDistance(elem, mouseX, mouseY) {
return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.offset().left+(elem.width()/2)), 2) + Math.pow(mouseY - (elem.offset().top+(elem.height()/2)), 2)));
}
/*- MOUSE STICKY -*/
function lerp(a, b, n) {
return (1 - n) * a + n * b
}
// Inizio Cursor
class Cursor {
constructor() {
this.bind()
//seleziono la classe del cursore
this.cursor = document.querySelector('.js-cursor')
this.mouseCurrent = {
x: 0,
y: 0
}
this.mouseLast = {
x: this.mouseCurrent.x,
y: this.mouseCurrent.y
}
this.rAF = undefined
}
bind() {
['getMousePosition', 'run'].forEach((fn) => this[fn] = this[fn].bind(this))
}
getMousePosition(e) {
this.mouseCurrent = {
x: e.clientX,
y: e.clientY
}
}
run() {
this.mouseLast.x = lerp(this.mouseLast.x, this.mouseCurrent.x, 0.2)
this.mouseLast.y = lerp(this.mouseLast.y, this.mouseCurrent.y, 0.2)
this.mouseLast.x = Math.floor(this.mouseLast.x * 100) / 100
this.mouseLast.y = Math.floor(this.mouseLast.y * 100) / 100
this.cursor.style.transform = `translate3d(${this.mouseLast.x}px, ${this.mouseLast.y}px, 0)`
this.rAF = requestAnimationFrame(this.run)
}
requestAnimationFrame() {
this.rAF = requestAnimationFrame(this.run)
}
addEvents() {
window.addEventListener('mousemove', this.getMousePosition, false)
}
on() {
this.addEvents()
this.requestAnimationFrame()
}
init() {
this.on()
}
}
const cursor = new Cursor()
cursor.init();
有没有办法在上面的 Studiomaertens 网站上做出类似的效果?
请告诉我
谢谢
【问题讨论】:
标签: javascript jquery cursor