我发现,为了使针停止动画反弹完成后,你需要做的针拖动。我发现这样做的最好方法是使用两个超时:
- 在第一次反弹完成之前删除动画。
- 使第一次反弹完成后标记不可拖动。
一旦您使标记不可拖动,动画将停止。我创建了一个plunker来说明我的意思:http://plnkr.co/edit/Gcns3DMklly6UoEJ63FP?p=preview
的HTML
<div id="map-canvas"></div>
<p>
Bounce marker
<select id="bounceNumber">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
times.
<button onclick="bounceMarker()">Go!</button>
</p>
JavaScript的
var marker = null,
toBounce = null,
toDrag = null;
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(-25.363882, 131.044922)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
marker = new google.maps.Marker({
position: new google.maps.LatLng(-25.363882, 131.044922),
map: map
});
}
function bounceMarker() {
var select = document.getElementById("bounceNumber"),
bounces = parseInt(select.options[select.selectedIndex].value),
animationTO = (bounces - 1) * 700 + 350,
dragTO = animationTO + 1000;
// Bounce the marker once
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
clearTimeout(toBounce);
clearTimeout(toDrag);
setTimeout(function() {
marker.setDraggable(false);
}, 750);
} else {
// Workaround to make marker bounce once.
// The api will finish the current bounce if a marker is set to draggable.
// So use two timeouts:
// 1. to remove the animation before the first bounce is complete.
// 2. to make the marker not draggable after the first bounce is complete.
// Animations will stop once you make a marker not draggable.
marker.setDraggable(true);
marker.setAnimation(google.maps.Animation.BOUNCE);
toBounce = setTimeout(function() {
marker.setAnimation(null);
}, animationTO);
toDrag = setTimeout(function() {
marker.setDraggable(false);
}, dragTO);
}
}
google.maps.event.addDomListener(window, 'load', initialize);
对于据我所知,这个工程跨浏览器。我已经在Chrome,Firefox,safari &歌剧中测试过。我还没有在Internet Explorer中测试过。
作为参考,二反弹是1400毫秒(不是1500,在第二次反弹后会稍微增加一点)。 – melat0nin
您可能需要等到地图完成加载,然后再启动反弹动画。 – sglessard