单击时的功能仅发生一次

function on click only occurs once

本文关键字:一次 功能 单击      更新时间:2023-09-26

我正在尝试在单击时放大或缩小地图。但是,首次单击后,地图无法再次缩放。我现在正在使用jquery animate,但如果有更好的解决方案,我也想知道。

  var mapWidth = $('.vector-map img').width();
  $('.zoom-in').on('click', function() {
    $('.vector-map img').animate({width: mapWidth + 50}, 'slow');
  });
  $('.zoom-out').on('click', function() {
    $('.vector-map img').animate({width: mapWidth - 50}, 'slow');
  });

只是在初始设置时获取地图宽度,而不是每次缩放之前,请尝试:

var mapWidth = $('.vector-map img').width();
  $('.zoom-in').on('click', function() {
    mapWidth = $('.vector-map img').width();
    $('.vector-map img').animate({width: mapWidth + 50}, 'slow');
  });
  $('.zoom-out').on('click', function() {
    mapWidth = $('.vector-map img').width();
    $('.vector-map img').animate({width: mapWidth - 50}, 'slow');
  });