Leaflet中使用leaflet-sidebar插件实现侧边栏效果
生活随笔
收集整理的這篇文章主要介紹了
Leaflet中使用leaflet-sidebar插件实现侧边栏效果
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
Leaflet快速入門與加載OSM顯示地圖:
Leaflet快速入門與加載OSM顯示地圖_BADAO_LIUMANG_QIZHI的博客-CSDN博客
在上面的基礎上,怎樣使用插件實現(xiàn)地圖側(cè)邊欄效果如下
注:
博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質(zhì)_CSDN博客-C#,SpringBoot,架構(gòu)之路領(lǐng)域博主
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費下載。
實現(xiàn)
1、插件地址
https://github.com/turbo87/leaflet-sidebar/
2、下載源碼,然后引入需要的L.Control.Sidebar.js文件
3、css文件不再單獨引入,直接放在html中
??????? .leaflet-sidebar {position: absolute;height: 100%;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 10px;z-index: 2000;}.leaflet-sidebar.left {left: -500px;transition: left 0.5s, width 0.5s;padding-right: 0;}.leaflet-sidebar.left.visible {left: 0;}.leaflet-sidebar.right {right: -500px;transition: right 0.5s, width 0.5s;padding-left: 0;}.leaflet-sidebar.right.visible {right: 0;}.leaflet-sidebar>.leaflet-control {height: 100%;width: 100%;overflow: auto;-webkit-overflow-scrolling: touch;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 8px 24px;font-size: 1.1em;background: white;box-shadow: 0 1px 7px rgba(0, 0, 0, 0.65);-webkit-border-radius: 4px;border-radius: 4px;}.leaflet-touch .leaflet-sidebar>.leaflet-control {box-shadow: none;border: 2px solid rgba(0, 0, 0, 0.2);background-clip: padding-box;}@media (max-width: 767px) {.leaflet-sidebar {width: 100%;padding: 0;}.leaflet-sidebar.left.visible~.leaflet-left {left: 100%;}.leaflet-sidebar.right.visible~.leaflet-right {right: 100%;}.leaflet-sidebar.left {left: -100%;}.leaflet-sidebar.left.visible {left: 0;}.leaflet-sidebar.right {right: -100%;}.leaflet-sidebar.right.visible {right: 0;}.leaflet-sidebar>.leaflet-control {box-shadow: none;-webkit-border-radius: 0;border-radius: 0;}.leaflet-touch .leaflet-sidebar>.leaflet-control {border: 0;}}@media (min-width: 768px) and (max-width: 991px) {.leaflet-sidebar {width: 305px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 305px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 305px;}}@media (min-width: 992px) and (max-width: 1199px) {.leaflet-sidebar {width: 390px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 390px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 390px;}}@media (min-width: 1200px) {.leaflet-sidebar {width: 460px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 460px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 460px;}}.leaflet-sidebar .close {position: absolute;right: 20px;top: 20px;width: 31px;height: 31px;color: #333;font-size: 25pt;line-height: 1em;text-align: center;background: white;-webkit-border-radius: 16px;border-radius: 16px;cursor: pointer;z-index: 8;}.leaflet-left {transition: left 0.5s;}.leaflet-right {transition: right 0.5s;}4、除了顯示地圖的div之外,再添加側(cè)邊欄顯示的內(nèi)容
<!-- 側(cè)邊欄彈窗顯示的內(nèi)容 --><div id="sidebar"><h1>公眾號</h1><p>霸道的程序猿</p><p><b>我的CSDN</b></p><p></p><ul><li><a href="https://blog.csdn.net/BADAO_LIUMANG_QIZHI">霸道流氓氣質(zhì)</a></li></ul></div>5、完整示例代碼,注釋見代碼
? <!doctype html> <html lang="en"><head><meta charset="UTF-8"><title>leaflet實現(xiàn)側(cè)邊欄效果</title><link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" /><style>html,body,#map {padding: 0;margin: 0;width: 100%;height: 100%;overflow: hidden;}.leaflet-sidebar {position: absolute;height: 100%;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 10px;z-index: 2000;}.leaflet-sidebar.left {left: -500px;transition: left 0.5s, width 0.5s;padding-right: 0;}.leaflet-sidebar.left.visible {left: 0;}.leaflet-sidebar.right {right: -500px;transition: right 0.5s, width 0.5s;padding-left: 0;}.leaflet-sidebar.right.visible {right: 0;}.leaflet-sidebar>.leaflet-control {height: 100%;width: 100%;overflow: auto;-webkit-overflow-scrolling: touch;-webkit-box-sizing: border-box;-moz-box-sizing: border-box;box-sizing: border-box;padding: 8px 24px;font-size: 1.1em;background: white;box-shadow: 0 1px 7px rgba(0, 0, 0, 0.65);-webkit-border-radius: 4px;border-radius: 4px;}.leaflet-touch .leaflet-sidebar>.leaflet-control {box-shadow: none;border: 2px solid rgba(0, 0, 0, 0.2);background-clip: padding-box;}@media (max-width: 767px) {.leaflet-sidebar {width: 100%;padding: 0;}.leaflet-sidebar.left.visible~.leaflet-left {left: 100%;}.leaflet-sidebar.right.visible~.leaflet-right {right: 100%;}.leaflet-sidebar.left {left: -100%;}.leaflet-sidebar.left.visible {left: 0;}.leaflet-sidebar.right {right: -100%;}.leaflet-sidebar.right.visible {right: 0;}.leaflet-sidebar>.leaflet-control {box-shadow: none;-webkit-border-radius: 0;border-radius: 0;}.leaflet-touch .leaflet-sidebar>.leaflet-control {border: 0;}}@media (min-width: 768px) and (max-width: 991px) {.leaflet-sidebar {width: 305px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 305px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 305px;}}@media (min-width: 992px) and (max-width: 1199px) {.leaflet-sidebar {width: 390px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 390px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 390px;}}@media (min-width: 1200px) {.leaflet-sidebar {width: 460px;}.leaflet-sidebar.left.visible~.leaflet-left {left: 460px;}.leaflet-sidebar.right.visible~.leaflet-right {right: 460px;}}.leaflet-sidebar .close {position: absolute;right: 20px;top: 20px;width: 31px;height: 31px;color: #333;font-size: 25pt;line-height: 1em;text-align: center;background: white;-webkit-border-radius: 16px;border-radius: 16px;cursor: pointer;z-index: 8;}.leaflet-left {transition: left 0.5s;}.leaflet-right {transition: right 0.5s;}</style> </head><body><!-- 側(cè)邊欄彈窗顯示的內(nèi)容 --><div id="sidebar"><h1>公眾號</h1><p>霸道的程序猿</p><p><b>我的CSDN</b></p><p></p><ul><li><a href="BADAO_LIUMANG_QIZHI的博客_霸道流氓氣質(zhì)_CSDN博客-C#,SpringBoot,架構(gòu)之路領(lǐng)域博主">霸道流氓氣質(zhì)</a></li></ul></div><div id="map"></div><script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script><script type="text/javascript" src="./js/L.Control.Sidebar.js"></script><script type="text/javascript">var map = L.map('map').setView([36.09, 120.35], 13);L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {attribution: ''}).addTo(map);//聲明側(cè)邊欄組件并添加到地圖var sidebar = L.control.sidebar('sidebar', {closeButton: true,position: 'left'});map.addControl(sidebar);//延時0.5秒后顯示側(cè)邊欄setTimeout(function () {sidebar.show();}, 500);//添加一個標記并設置其點擊事件,點擊時顯示側(cè)邊欄var marker = L.marker([36.09, 120.35]).addTo(map).on('click', function () {sidebar.toggle();});//設置地圖點擊事件,側(cè)邊欄隱藏map.on('click', function () {sidebar.hide();})//側(cè)邊欄顯示時的事件sidebar.on('show', function () {console.log('Sidebar will be visible.');});//側(cè)邊欄顯示成功之后的事件sidebar.on('shown', function () {console.log('Sidebar is visible.');});//隱藏側(cè)邊欄時的事件sidebar.on('hide', function () {console.log('Sidebar will be hidden.');});//側(cè)邊欄隱藏成功后的事件sidebar.on('hidden', function () {console.log('Sidebar is hidden.');});//側(cè)邊欄的關(guān)閉按鈕的點擊事件L.DomEvent.on(sidebar.getCloseButton(), 'click', function () {console.log('Close button clicked.');});</script> </body></html>?總結(jié)
以上是生活随笔為你收集整理的Leaflet中使用leaflet-sidebar插件实现侧边栏效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet中使用Leaflet.Sp
- 下一篇: Leaflet中使用Leaflet-Mi