对于选定的元素,在悬停/鼠标悬停时停止动画?

发布于 2024-08-24 22:26:14 字数 3133 浏览 6 评论 0原文

我正在尝试复制 http://www.fiat 上看到的效果。 co.uk/Showroom/#showroom/punto_evo/explore

我制作了一个函数来对“兴趣点”标记进行动画处理,这些标记绝对位于父 div 内,当这些标记悬停在上面时,会显示子 div。

然而,我正在努力将 .stop() 函数添加到悬停时的动画中,因此“标记”及其内容是静态的,而未悬停的“标记”则继续动画。

我已经尝试过了;

$("#triggers a").mouseover(function(){$(this).stop();})

但这似乎不起作用。

任何帮助将不胜感激,谢谢。

我的代码如下;

CSS

#triggers {
text-align:center;
height: 200px;
position: relative;
width: 500px;
margin-right: auto;
margin-left: auto;
margin-top: 100px;
}
#triggers a {
cursor: pointer;
height: 50px;
width: 50px;
background-image: url(../assets/images/Arrow3%20Right.png);
background-repeat: no-repeat;
}
#feature1 {
position: absolute;
height: auto;
width: auto;
left: 10px;
top: 10px;
}
#feature2 {
position: absolute;
height: 10px;
width: 10px;
left: 45px;
top: 150px;
}
#feature3 {
position: absolute;
top: 45px;
right: 55px;
}
#feature4 {
position: absolute;
top: 60px;
right: 200px;
}
#feature5 {
position: absolute;
top: 67px;
height: 10px;
width: 10px;
left: 150px;
}
.feature-box {
background-color: #F00;
height: auto;
width: 150px;
position: absolute;
left: -50px;
top: -50px;
color: #FFF;
font-family: Arial, Helvetica, sans-serif;
display: none;
}
.feature-header {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}

HTML 脚本

<div id="triggers">

<a id="feature1">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature2">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature3">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature4">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature5">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

</div>

<script type="text/javascript">
$(document).ready(function() {

function move_box() {
$("#triggers a").animate({"marginTop": "-=10px"},500).animate({"marginTop": ""},500, move_box);
}   

move_box();

$('#triggers a').hover(function() {

$(this).children("div").show();
$(this).toggleClass('active');

}, function() {
$(this).removeClass('active');
$(".feature-box").fadeOut();
});

});
</script>

Im trying to replicate an effect as seen on http://www.fiat.co.uk/Showroom/#showroom/punto_evo/explore.

I have made a function to animate the 'point of interest' markers which are absolutely posistioned within a parent div and when these markers are hovered over, the child div is shown.

However i am struggling with adding the .stop() function to the animation on hover, so the 'marker' and its contents are static, whist the un-hovered 'markers' continue animating.

I have tried;

$("#triggers a").mouseover(function(){$(this).stop();})

but this does'nt seem to work.

Any help would be greatly appreciated, thanks.

My code is as follows;

CSS

#triggers {
text-align:center;
height: 200px;
position: relative;
width: 500px;
margin-right: auto;
margin-left: auto;
margin-top: 100px;
}
#triggers a {
cursor: pointer;
height: 50px;
width: 50px;
background-image: url(../assets/images/Arrow3%20Right.png);
background-repeat: no-repeat;
}
#feature1 {
position: absolute;
height: auto;
width: auto;
left: 10px;
top: 10px;
}
#feature2 {
position: absolute;
height: 10px;
width: 10px;
left: 45px;
top: 150px;
}
#feature3 {
position: absolute;
top: 45px;
right: 55px;
}
#feature4 {
position: absolute;
top: 60px;
right: 200px;
}
#feature5 {
position: absolute;
top: 67px;
height: 10px;
width: 10px;
left: 150px;
}
.feature-box {
background-color: #F00;
height: auto;
width: 150px;
position: absolute;
left: -50px;
top: -50px;
color: #FFF;
font-family: Arial, Helvetica, sans-serif;
display: none;
}
.feature-header {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}

HTML

<div id="triggers">

<a id="feature1">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature2">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature3">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature4">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

<a id="feature5">
<div class="feature-box">
<p class="feature-header">This is information for this point of interest</p>
<p>This is some more information</p>
</div>
</a>

</div>

SCRIPT

<script type="text/javascript">
$(document).ready(function() {

function move_box() {
$("#triggers a").animate({"marginTop": "-=10px"},500).animate({"marginTop": ""},500, move_box);
}   

move_box();

$('#triggers a').hover(function() {

$(this).children("div").show();
$(this).toggleClass('active');

}, function() {
$(this).removeClass('active');
$(".feature-box").fadeOut();
});

});
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2024-08-31 22:26:14

如果您将动画重构为每个元素,这可能是一种更简单的方法,看看这是否符合您的要求:

<script type="text/javascript">
  function move_box(elem) {
    elem.animate({"marginTop": "-=10px"}, 500)
    .animate({"marginTop": ""}, 500, function() { move_box($(this)) });
  }

  $(document).ready(function() {
    $("#triggers a").each(function() {
      move_box($(this)); //Start animation on each
    });

    $('#triggers a').hover(function() {
      //Stop this element's animation
      $(this).stop().toggleClass('active').children("div").show();
    }, function() {
      $(this).removeClass('active');
      $(".feature-box").fadeOut();
      move_box($(this)); //Resume animation only on this.
    });
  });
</script>

If you refactored your animations to be per-element it might be a simpler approach, see if this does what you want:

<script type="text/javascript">
  function move_box(elem) {
    elem.animate({"marginTop": "-=10px"}, 500)
    .animate({"marginTop": ""}, 500, function() { move_box($(this)) });
  }

  $(document).ready(function() {
    $("#triggers a").each(function() {
      move_box($(this)); //Start animation on each
    });

    $('#triggers a').hover(function() {
      //Stop this element's animation
      $(this).stop().toggleClass('active').children("div").show();
    }, function() {
      $(this).removeClass('active');
      $(".feature-box").fadeOut();
      move_box($(this)); //Resume animation only on this.
    });
  });
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文