需求描述
- 【问题】点击地图标点弹出信息框,如果标点靠近浏览器窗口边界,信息框就会被遮挡,
- 【需求】靠近浏览器边界的信息框展示时反方向展示
- 【解决】
地图容器宽/高 - 点击标点位置的x/y
是否小于信息框宽/高,小于的话 信息框的 left / top 减去信息框宽/高
代码展示
addMakersToMap() {
let layers = []
this.markers.forEach((item) => {
if (item.jd && item.wd) {
var marker = new L.marker([item.wd, item.jd], {
...
}).on('click', (e) => {
console.log('e----', e)
console.log('"点击图标"----', '点击图标')
setTimeout(() => {
const clickX = e.containerPoint.x
const clickY = e.containerPoint.y
const mapDom = document.getElementById('map')
const mapDomWidth = mapDom.offsetWidth
const mapDomHeight = mapDom.offsetHeight
const popupDom = document.getElementsByClassName('popup')[0]
const popupDomWidth = popupDom.offsetWidth
const popupDomHeight = popupDom.offsetHeight
const iconWidth = document.getElementsByClassName('leaflet-marker-icon')[0].offsetWidth + 10
const isOverflowRight = mapDomWidth - clickX < popupDomWidth
const isOverflowBottom = mapDomHeight - clickY < popupDomHeight
const rightToLeft = (65 - popupDomWidth - iconWidth) + 'px'
const bottomTotop = (10 - popupDomHeight) + 'px'
if(isOverflowRight) {
popupDom.style.left = rightToLeft
}
if(isOverflowRight && isOverflowBottom) {
popupDom.style.left = rightToLeft
popupDom.style.top = bottomTotop
}
if(isOverflowBottom) {
popupDom.style.top = bottomTotop
}
}, 10);
})
...
...
}
})
...
},
效果展示