单击地图标记时平滑滚动
Smooth scroll when click a map marker
我有一个地图,我显示多个标记,如果一个标记被点击,你去到页面底部的div(经典锚链接)
有人能告诉我为什么我没有平滑滚动时,我点击标记,但我确实有,如果我只是创建一个链接和一个div的ID?
这是js:
jQuery(function($) {
// Asynchronously Load the map API
var script = document.createElement('script');
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
document.body.appendChild(script);
});
function initialize() {
var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
mapTypeId: 'roadmap'
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
map.setTilt(45);
// Multiple Markers
var markers = [
['London Eye, London', 51.503454,-0.119562, '#1'],
['Palace of Westminster, London', 51.499633,-0.124755, '#2']
];
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;
// Loop through our array of markers & place each one on the map
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
url: markers[i][3]
});
google.maps.event.addListener(marker, 'click', function() {
window.location.hash = this.url;
});
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
this.setZoom(14);
google.maps.event.removeListener(boundsListener);
});
}
我猜'juping'是由这段代码引起的:
google.maps.event.addListener(marker, 'click', function() {
window.location.hash = this.url;
});
删除事件监听器:
google.maps.event.addListener(marker, 'click', function() {
和这个
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
并添加这段代码,它将通过marker.url
data获取html标签并对它们进行动画滚动:
google.maps.event.addListener(marker, 'click', function() {
var elem = $(marker.url);
$('html, body').animate({
scrollTop: elem.offset().top
}, 1000 );
});
它将平滑滚动到你的div:
<div id='1'>1</div>
<div id='2'>2</div>
演示小提琴:http://jsfiddle.net/o8u13whg/
EDIT: close fix
评论中描述的问题:我刚刚注意到,现在它总是向下滚动到最后一个ID…不管你点击什么标记。是由于闭包处理不当造成的,导致总是最后 marker.url
被滚动到。
要修复它,将click
事件从for
循环中拉出:
for( i = 0; i < markers.length; i++ ) {
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
marker = new google.maps.Marker({
position: position,
map: map,
title: markers[i][0],
url: markers[i][3]
});
attachClickHandler(marker);
.....
,然后在attachClickHandler(marker)
函数中执行滚动魔术:
function attachClickHandler(marker){
google.maps.event.addListener(marker, 'click', function() {
var elem = $(marker.url);
$('html, body').animate({
scrollTop: elem.offset().top
}, 1000 );
});
}
演示小提琴:http://jsfiddle.net/o8u13whg/3/
闭包可能是一个棘手的事情:)
相关文章:
- Safari(Mac OS)上的jQuery平滑滚动问题
- jquery平滑滚动问题
- javascript mootools平滑滚动'x'像素数量
- jQuery平滑滚动
- jQuery平滑滚动未滚动
- 更新Wordpress和我的平滑滚动停止工作
- 具有子菜单和平滑滚动的粘性导航
- 平滑滚动&懒惰
- 平滑滚动时更改 ID 的颜色
- 使用我的基于标签的平滑滚动脚本而不带“a”的解决方案
- 调整平滑滚动代码
- 使用 window.scrollBy) 平滑滚动
- 平滑滚动代码
- 如何在ng视图中进行页面渲染后的平滑滚动
- 如何使用requestAnimationFrame实现平滑滚动
- 如何由于浏览器的平滑滚动而忽略大量滚动事件
- 使用普通JavaScript平滑滚动到元素
- 平滑滚动并突出显示导航问题
- 在函数goToNext上平滑滚动+偏移
- 如何在HTML5页面上启用平滑滚动