【问题标题】:problems trying to use jquery to zoom and pan an image map尝试使用 jquery 缩放和平移图像地图的问题
【发布时间】:2010-09-16 23:17:32
【问题描述】:

~我已经为此工作了一段时间(好几个月了!!)——使用 jquery 来缩放和平移图像地图——这有点超出我目前的技能水平,所以我很高兴我有已经走到这一步了!!~但是功能并没有它需要的那么流畅(放大和缩小需要更流畅)——在IE7中查看时会出现一些更大的问题。

在网页上:http://www.agencydr.squarespace.com/locations

下面是使用的jquery。 有人注意到我应该改变什么吗?

/* ************************************** */
/* locations map */
/* ************************************** */
$('#modulePage6955407 #sectionContent3019160').insertAfter("#pageHeaderWrapper");
$('#modulePage6955407 #bannerAreaWrapper').animate({"top": "+=250px"}, "slow");
$('#modulePage6955407 #sectionContent3019160').animate({"height": "290px"}, "slow");
$('#modulePage6955407 #sectionContent3019160').animate({"opacity": "1"}, "slow");

$('#LocationsMapWrapper').hover(function() {
   $('#LocationsMapWrapper #MapImage').animate({
      width: 600,
      height: 375,
      marginLeft: 550,
      marginTop: -20
   }, "slow", "easeOutQuad");
 },
 function() {
   $('#LocationsMapWrapper #MapImage').animate({
      width: 478, 
      height: 265, 
      marginLeft: 480, 
      marginTop: 0
   }, "slow", "easeOutQuad");

}).mousemove(function(e){
    $("#LocationsMapWrapper #MapImage").each(function(){
        var position = $(this).offset();
        var position_x = position.left;
        var position_y = position.top;
        position_x = 0;
        position_y = 0;
        var windowwidth = ($(window).width()/2);
        var windowheight = ($("#LocationsMapWrapper").height()/2);
        var endX = (windowwidth)-(e.pageX);
        var endY = (windowheight)-(e.pageY);
        var speed = 5;
        position_x += (endX-position_x)/speed;
        position_y += (endY-position_y)/speed;
        $(this).css({'position': 'relative'});
        $(this).css({'left':position_x});
        $(this).css({'top':position_y});
   }); 
}).mouseleave(function(){
   $("#LocationsMapWrapper #MapImage").each(function(){
        $(this).css({'left':0});
        $(this).css({'top':0});
   }); 
});

这里是一些相关的 css:

/* locations map */
#sectionContent3019160 { width: 100%; height: 0px; margin: auto; opacity: 0.15; margin-bottom: 10px; z-index: 1; overflow: hidden; }
#sectionContent3019160 { margin-top: -48px; } /* up */

#LocationsMapWrapper { width: 1020px; height: 355px; margin: auto; }
#LocationsMapWrapper #MapImage { width: 478px; height: 265px; margin-left: 480px; }

任何帮助表示赞赏!我已经尝试了我能想到的一切来让它更顺利地运行,但我不知所措。请注意,我使用的是托管 CMS 服务,因此我必须修改一些已建立的 html 布局。

更新: 这是我目前更新的代码:

$('#LocationsMapWrapper').hover(function() {
   $('#LocationsMapWrapper #MapImage').stop().animate({ 
      width: 600,
      height: 375,
      marginLeft: 550,
      marginTop: -20
   }, "slow", "easeOutQuad").mousemove(function(e){
        var position = $(this).offset();
        var position_x = position.left;
        var position_y = position.top;
        position_x = 0;
        position_y = 0;
        var windowwidth = ($(window).width()/2);
        var windowheight = ($("#LocationsMapWrapper").height()/2);
        var endX = (windowwidth)-(e.pageX);
        var endY = (windowheight)-(e.pageY);
        var speed = 5;
        position_x += (endX-position_x)/speed;
        position_y += (endY-position_y)/speed;
        $(this).css({'position': 'relative'});
        $(this).css({'left':position_x});
        $(this).css({'top':position_y});
   });         
}, function() {
   $('#LocationsMapWrapper #MapImage').stop().animate({ 
      width: 478, 
      height: 290, 
      marginLeft: 480, 
      marginTop: 0,
      left: 0,
      top: 0
   }, "slow", "easeOutQuad");
});

【问题讨论】:

  • .animate() 更改为.stop().animate() 似乎对某些人有所帮助。我也再次更新了我的代码......但仍然没有弄清楚如何在动画运行之前添加计时器。
  • 你看过 jQuery 的 hoverintent 插件吗?虽然有时可能不需要聪明或独创性,但总是需要勤奋。
  • 另外,一个很好的练习是逐行检查你的代码,并在代码行旁边写一个注释来解释那里正在做什么以及为什么。这样你可能会更好地发现错误。例如,在这一行中正在做什么? position_x += (endX-position_x)/速度;

标签: jquery hover zooming imagemap mousemove


【解决方案1】:

只有当鼠标在图像上徘徊,几乎一动不动时,您才想要缩放它。您不希望图像仅因偶然的鼠标接触而缩小。

我建议您在鼠标悬停在图像上时启动一个计时器,它会在大约 250-500 毫秒后调用缩放功能,前提是鼠标在该时间间隔内移动的距离不超过设定的阈值距离。如果鼠标移动超过您的阈值距离(编辑:仍然在图像上,或从图像上移开),请终止计时器,以免发生缩放。

【讨论】:

  • 哦,听起来很有希望!我将尝试弄清楚如何做到这一点 - 希望我能得到它,当然以前没有写过这样的脚本,所以我可能需要一段时间来反复试验!
  • 由于某种原因,在 Internet Explorer 7 中,地图图像跳出它的包含元素(如溢出:隐藏;不工作)。它还会在页面中跳转,并且比其他浏览器更容易出错。
  • 我不知道该怎么做!~
  • 还有一个jQuery插件可能对你有用(我自己没机会用过),叫做hoverintent。
猜你喜欢
  • 2013-09-29
  • 2010-10-19
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-09-10
相关资源
最近更新 更多