jQuery animate height of Google Map container

旧时模样 提交于 2019-12-11 03:56:32

问题


I have a map container DIV. Lets call it #mapcontainer.

I animate its height with jQuery:

$("#mapcontainer").animate({height: +=200px}, 500);

I know that if a map container is resized google maps needs to be triggered to be resized using:

google.maps.event.trigger(map, 'resize');

Now the obvious thing is to do this in the callback of animate:

$("#mapcontainer").animate({height: +=200px}, 500, "linear", function(){
    google.maps.event.trigger(map, 'resize');
});

But this doesn't look smooth and the map just juts to the hight after the animation has completed.

Is there anyway that the map can be resizing as the animation is running so the map transitions smoothly?


回答1:


You trigger the event after the animation is complete and I think it's the issue.

It's not a good solution but maybe you can use setInterval and trig the event every 1ms when the animation is running. Maybe it it will lag, and as I said its not a best-practies solution.

var timer;

timer = setInterval(function(){ google.maps.event.trigger(map, 'resize')}, 100);
$("#mapcontainer").animate({height: +=200px}, 500, "linear", function(){
    timerMap = clearInterval(timer);
});


来源:https://stackoverflow.com/questions/9210260/jquery-animate-height-of-google-map-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!