Jquery/JS css动画在悬停时工作错误
Jquery/JS css animation works wrong on hover
我正在尝试让屏幕上的一些点动画化,它们应该在悬停时停止。
代码:
function dot(_options) {
this.id = _options.id;
this.speedX = 0;
this.speedY = 0;
this.posx = _options.posx;
this.posy = _options.posy;
this.width;
this.height;
this.animation = true;
};
dot.prototype.animationStep = function() {
if(this.animation == true) {
while (this.speedX == 0)
this.speedX = randPos(-3, 3);
while (this.speedY == 0)
this.speedY = randPos(-3, 3);
if (this.posx + this.speedX <= 0 || this.posx + this.speedX + this.width >= $(window).width()-2){
if(this.speedX < 0){
this.speedX = randPos(1.5, 3);
}
else {
this.speedX = randPos(-1.5, -3);
}
}
if (this.posy + this.speedY <= 0 || this.posy + this.speedY + this.height >= $(window).height()-85){
if(this.speedY < 0){
this.speedY = randPos(1, 3);
}
else {
this.speedY = randPos(-1, -3);
}
}
this.posx += this.speedX;
this.posy += this.speedY;
this.width = $('#dot' + this.id).width();
this.height = $('#dot' + this.id).height();
$('#dot' + this.id).parent().css({
'left': this.posx + 'px',
'top': this.posy + 'px'
});
// $('#dot' + this.id).html(this.speedX + ' / ' + this.speedY);
}
}
dot.prototype.pause = function(){
this.speedX = 0;
this.speedY = 0;
}
dot.prototype.unpause = function() {
while (this.speedX == 0)
this.speedX = randPos(-3, 3);
while (this.speedY == 0)
this.speedY = randPos(-3, 3);
}
var dots = [];
dots.push(new dot({
id: 1,
posx: 100,
posy: 100
}));
dots.push(new dot({
id: 2,
posx: 100,
posy: 100
}));
dots.push(new dot({
id: 3,
posx: 100,
posy: 100
}));
dots.push(new dot({
id: 4,
posx: 100,
posy: 100
}));
dots.push(new dot({
id: 5,
posx: 100,
posy: 100
}));
dots.push(new dot({
id: 6,
posx: 100,
posy: 100
}));
window.setInterval(function() {
for (var i = 0; i < dots.length; i++) {
dots[i].animationStep();
}
}, 16);
function randPos(max, min) {
return (Math.random() * (max - min + 1)) + min;
}
$(document).ready(function(){
$('.dot').hover(
function(){
var dataid = $('div', this).attr('data-id');
dots[dataid-1].animation=false;
},
function(){
var dataid = $('div', this).attr('data-id');
dots[dataid-1].animation=true;
});
$('.dot div').hover(
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=false;
},
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=true;
});
$('.div2').hover(
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=false;
},
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=true;
});
$('.color').hover(
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=false;
},
function(){
var dataid = $(this).attr('data-id');
dots[dataid-1].animation=true;
});
});
在Firefox和Safari(Windows)中,它可以正常工作。这里只有一个小的影响,点飞离光标。但在Chrome、IE和Safari(Mac)中,你无法捕捉到光标前面飞了很远的点,我不知道为什么。
我希望你们中的一些人能帮助我。
这是我的小提琴
非常感谢!
我解决了。我在css里给a加了一个transition: 0.3s
。这给了我一个错误,特别是干扰chrome和mac safari。
相关文章:
- 为什么jquery悬停在jsfiddle中可以工作,而在我的html布局中却不能
- 链接don'由于Javascript悬停效果,无法在移动设备上工作
- 链接悬停css在不浮动时不在表浮动标题中工作
- 在jquery中切换类不在悬停中工作
- 努力让第n个有悬停的孩子在IE工作
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- 切换悬停和单击无法正常工作
- fadeIn在悬停时工作,但在单击/单击时不工作
- Javascript JIRA扩展无法在悬停时工作
- replaceWith&fadeIn/fadeOut-悬停后无法工作
- 只显示潜水时悬停按钮X时间-可以'我不能使它正常工作
- css img:悬停工作,img:活动无效
- jQuery悬停只工作一次
- 停止动画停止工作,jQuery/悬停
- 如何将悬停事件添加到 SVG 中的多个路径?并让它在IE9中工作
- 悬停添加类在Chrome上出现故障,但在Firefox上工作正常
- 打开和悬停不一起工作
- 鼠标悬停在整个页面上工作,而不是一个元素
- jQuery 悬停在几秒钟后停止工作
- 为什么只有第一个鼠标悬停和鼠标悬停工作?