Google 地图信息框中的图像滑块
当用户单击地图上的标记时,我会弹出信息框(具有更多控件的信息窗口)。在此弹出窗口中,我当前正在尝试插入 jQuery 图像滑块,特别是 NivoSlider。
问题: 但是,此图像滑块仅在普通 HTML 页面上有效,而在信息框内根本不起作用。我需要图像在信息框中内部工作。
jQuery/JS 代码
加载 jQuery 图像滑块的部分由 "
$(window).load(function() {
$('#slider').nivoSlider(); //加载div #slider中的nivoslider
$("#search_button").click(function(e){ e.preventDefault(); var search_location = $("#search_location").val(); $.getJSON(........................, function() {
for( i = 0; i < json.length; i++) {
// Place markers on map
var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
var marker = new google.maps.Marker({
position: latLng,
map: map
});
markers.push(marker);
// Create infowindows
var boxText = '<div id="infobox"> \
<div id="infobox_header_title"> \
<span id="infobox_header_price">$' + json[i].price + '</span> \
<span id="infobox_header_room">' + json[i].bedroom + 'br </span> \
<span id="infobox_header_address">' + json[i].address_1 + '</span> \
</div> \
<div id="slider" class="nivoSlider"> \
<img src="/images/cl/' + json[i].photos[0] + '.jpg" /> \
<img src="/images/cl/' + json[i].photos[1] + '.jpg" /> \
</div> \
</div>';
var infoboxOptions = {
content: boxText,
disableAutoPan: false,
maxWidth: 0,
pixelOffset: new google.maps.Size(-140, 0),
zIndex: null,
boxStyle: {
},
closeBoxMargin: "10px 2px 2px 2px",
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
infoBoxClearance: new google.maps.Size(1, 1),
isHidden: true,
pane: "floatPane",
enableEventPropagation: false
};
var infobox = new InfoBox(infoboxOptions);
infobox.open(map, markers[i]);
infoboxes.push(infobox);
// Create Listeners
markers[i]._index = i;
google.maps.event.addListener(markers[i], 'click', function() {
//infoboxes[this._index].open(map, markers[i]);
infoboxes[this._index].show();
});
};
// Fill screen with markers
mapAutoCenter(markers);
});
我认为导致此问题的原因: 可能是某些原因与在开始时调用 $('#slider').nivoSlider()
时未创建信息框中的
相关附加信息:我正在使用 Google Maps API V3,以及 PHP 和 Codeigniter 框架。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
滑块通过绑定一个事件监听器来工作,当 infoWindow 从 DOM 树中删除时,该事件监听器将会丢失(Google 地图在 infoWindow 不可见时从 DOM 树中删除它)。即使信息窗口出现,监听者仍然会消失。
您必须重写 NivoSlider 插件的滑块侦听器部分。这里有一些指导。
编写一个单击处理程序、一个鼠标移动处理程序和一个鼠标向上处理程序函数,并将其放在某处(可能在 NivoSlider 插件内,这样就不会污染全局空间)。像这样的事情:
现在,直接在 HTML 中将 sliderClickHandler 注册为
onclick
事件:注意,您不能执行这样的操作:
因为您将侦听器直接放入 DOM 元素中,这将导致从全局空间执行(并且不会有任何其他名称空间绑定到它,这与典型的 javascript 不同,我们习惯于将函数作为闭包并封装/记住它们的定义位置)。
这种解决方法有一个主要问题(但我想不出更好的方法来做到这一点)。如果您的页面上有多个 NivoSlider(您可能会这样做),那么您自然会希望处理程序函数根据您正在与哪个 NivoSlider 交互而表现不同。由于您无法在每个实例上作为闭包动态创建处理程序,因此您必须为每个 NivoSlider 拥有唯一的 ID,并且处理程序必须根据当前活动对象的 ID 进行操作。 (因此您必须将当前活动的 NivoSlider 的 ID 存储在某处)。
祝你好运。绝对可行,尽管不是很有趣。
The slider works by binding an event listener, which will be lost when the infoWindow is removed from the DOM tree (Google maps removes the infoWindow from the DOM tree when it is not visible). Even when the infoWindow appears, the listener will still be gone.
You will have to rewrite the slider listener part of your NivoSlider plugin. Here's some guidance.
Write a click handler, mousemove handler and a mouseup handler function and put it somewhere (probably inside the the NivoSlider plugin so you don't pollute the gloal space). Something like this:
Now, register the sliderClickHandler directly in the HTML as an
onclick
event:Notice, you couldn't do something like this:
because you're putting the listener directly into the DOM element which will be executing from the global space (and will not have any other namespace bound to it, unlike typical javascript where we are so used to functions being closures and encapsulating/remembering where they were defined).
There is one major issue with this workaround (but I can't think of a better way to do it). If you will have more than one NivoSlider on your page (which you probably will), then naturally you will want the handler functions to behave differently depending on which NivoSlider you are interacting with. Since you can't create handlers on the fly as closures on each instance, you will have to have unique ID's for each NivoSlider and the handler's will have to act depending on which ID is currently the active object. (So you'll have to store the currently active NivoSlider's ID somewhere).
Good luck. Definitely doable, though not very fun.