Created
August 19, 2012 23:06
-
-
Save glenrobertson/3398417 to your computer and use it in GitHub Desktop.
leaflet icon hover class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| L.Icon.Hover = L.Icon.extend({ | |
| options: { | |
| iconSize: new L.Point(25, 41), | |
| iconAnchor: new L.Point(13, 41), | |
| popupAnchor: new L.Point(1, -34), | |
| shadowSize: new L.Point(41, 41) | |
| }, | |
| initialize: function (options) { | |
| L.Util.setOptions(this, options); | |
| }, | |
| _createIcon: function (name) { | |
| var img = L.Icon.prototype._createIcon.call(this, name); | |
| var hoverUrl = this._getIconHoverUrl(); | |
| var standardUrl = this._getIconUrl(name); | |
| if (name === 'icon') { | |
| img.onmouseover = function() { | |
| this.src = hoverUrl; | |
| }; | |
| img.onmouseout = function() { | |
| this.src = standardUrl; | |
| }; | |
| } | |
| return img; | |
| }, | |
| _getIconUrl: function (name) { | |
| var key = name + 'Url'; | |
| if (this.options[key]) { | |
| return this.options[key]; | |
| } | |
| var path = L.Icon.Default.imagePath; | |
| if (!path) { | |
| throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually."); | |
| } | |
| return path + '/marker-' + name + '.png'; | |
| }, | |
| _getIconHoverUrl: function () { | |
| var key = 'iconHoverUrl'; | |
| if (this.options[key]) { | |
| return this.options[key]; | |
| } | |
| var path = L.Icon.Default.imagePath; | |
| if (!path) { | |
| throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually."); | |
| } | |
| return path + '/marker-' + name + '-hover.png'; | |
| } | |
| }); | |
| L.Icon.Default.imagePath = (function () { | |
| var scripts = document.getElementsByTagName('script'), | |
| leafletRe = /\/?leaflet[\-\._]?([\w\-\._]*)\.js\??/; | |
| var i, len, src, matches; | |
| for (i = 0, len = scripts.length; i < len; i++) { | |
| src = scripts[i].src; | |
| matches = src.match(leafletRe); | |
| if (matches) { | |
| return src.split(leafletRe)[0] + '/images'; | |
| } | |
| } | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment