googlemaps-scrollprevent
googlemaps-scrollprevent copied to clipboard
:lock: googlemaps-scrollprevent avoid unwanted map interactions with the Google Maps Iframe.
googlemaps-scrollprevent.js
Avoid unwanted map interactions with the Google Maps Iframe.
Disable mouse scroll wheel zoom on embedded Google Maps
googlemaps-scrollprevent is an easy solution to the problem of page scrolling with new "Google Maps Iframe Embed".
This jQuery Mobile First plugin prevents Google Maps iframe from capturing the mouse's scrolling wheel / touch scrolling behavior wrapping the <iframe>
with a transparent <div>
on mouse / touch hover, so you must click / tap the unlock button to toggle the normal navigation. See the Live Demo.
This jQuery plugin is written with CoffeeScript that compiles in JavaScript, so the source files are a little different from standard JavaScript.
Please if you are using this plugin open an Issue with the tag showcase
. If this plugin was helpful for you saving some time and effort. Can consider donate as a thank you. Thanks!
P.S.
You can find simpler approaches in stackoverflow like:
- How to disable mouse scroll-wheel scaling with Google Maps API
- Disable mouse scroll wheel zoom on embedded Google Maps
- Prevent a Google Maps iframe from capturing the mouse's scrolling wheel behavior
Details
Minimun Requeriments
Table of contents
- Examples
- Usage as JQuery Plugin
- Usage in Wordpress
- Default Options
- Build From Source
- License
Examples
For usage examples check the live demo.
Usage as jQuery plugin
-
Include jQuery and googlemaps-scrollprevent Libs in your html.
<!-- html --> <head> // jQuery Google CDN <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js?ver=2.1.4"></script> // googlemaps-scrollprevent rawgit CDN <script type="text/javascript" src="https://cdn.rawgit.com/diazemiliano/googlemaps-scrollprevent/master/dist/googlemaps-scrollprevent.min.js"></script> </head>
-
Start mapScrollPrevent including the following code.
<!-- html --> <script type="text/javascript"> $(function() { // Only Google Maps Selector var googleMapSelector = "iframe[src*=\"google.com/maps\"]"; $(googleMapSelector).scrollprevent().start(); }); </script>
Or Stop with:
// JavaScript // Stop $(googleMapSelector).scrollprevent().stop();
-
Edit defaults.
<!-- html --> <script type="text/javascript"> $(function() { // Only Google Maps Selector var googleMapSelector = "iframe[src*=\"google.com/maps\"]"; var options = { pressDuration: 1000 }; $(googleMapSelector).scrollprevent(options).start(); }); </script>
-
With Callbacks
// JavaScript $(function(){ $("#btn-start").click(function(){ $("iframe[src*='google.com/maps']").scrollprevent({ onMapLock: function() { // Your code here. alert("Map Locked") }, onMapUnlock: function() { // Your code here. alert("Map Unlocked") } }).start(); }); });
Usage in Wordpress
- Enqueue a script with jQuery as a dependency in yout
functions.php
```php
// PHP
// First Enqueue the plugin
function mapScrollPrevent_plugin() {
wp_enqueue_script( 'mapScrollPrevent', 'https://cdn.rawgit.com/diazemiliano/mapScrollPrevent/master/dist/mapScrollPrevent.min.js', array( 'jquery' ) , '0.6.4', true );
}
// Second Enqueue the script
function mapScrollPrevent_script()
{
echo '
<script type="text/javascript">
$(function() {
var googleMapSelector = "iframe[src*=\"google.com/maps\"]";
var options = { pressDuration: 1000 };
$(googleMapSelector).scrollprevent(options).start();
});
</script>
';
}
// Do the hook
add_action( 'wp_enqueue_scripts', 'mapScrollPrevent_plugin' );
add_action( 'wp_head', 'mapScrollPrevent_script' );
```
Default Options
// JavaScript
var options = {
"class": {
/* class for map wrap */
wrap: "mapscroll-wrap",
/* class for hover div */
overlay: "mapscroll-overlay",
/* class for progress bar */
progress: "mapscroll-progress",
/* class for the unlock button */
button: "mapscroll-button",
/* class for svg icons */
icon: "mapscroll-icon"
},
/* Press Duration */
pressDuration: 650,
/* Unlock Trigger (overlay|button) */
triggerElm: "button",
/* Buton Icons */
overlay: {
iconLocked: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"22\" height=\"22\" viewBox=\"0 0 1792 1792\" > <path transform=\"translate(1)\" d=\"M640 768h512v-192q0-106-75-181t-181-75-181 75-75 181v192zm832 96v576q0 40-28 68t-68 28h-960q-40 0-68-28t-28-68v-576q0-40 28-68t68-28h32v-192q0-184 132-316t316-132 316 132 132 316v192h32q40 0 68 28t28 68z\" /> </svg>",
iconUnloking: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"22\" height=\"22\" viewBox=\"0 0 1792 1792\"> <path transform=\"translate(1)\" d=\"M1376 768q40 0 68 28t28 68v576q0 40-28 68t-68 28h-960q-40 0-68-28t-28-68v-576q0-40 28-68t68-28h32v-320q0-185 131.5-316.5t316.5-131.5 316.5 131.5 131.5 316.5q0 26-19 45t-45 19h-64q-26 0-45-19t-19-45q0-106-75-181t-181-75-181 75-75 181v320h736z\" /> </svg>",
iconUnlocked: "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"22\" height=\"22\" viewBox=\"0 0 1792 1792\"> <path transform=\"translate(1)\" d=\"M1728 576v256q0 26-19 45t-45 19h-64q-26 0-45-19t-19-45v-256q0-106-75-181t-181-75-181 75-75 181v192h96q40 0 68 28t28 68v576q0 40-28 68t-68 28h-960q-40 0-68-28t-28-68v-576q0-40 28-68t68-28h672v-192q0-185 131.5-316.5t316.5-131.5 316.5 131.5 131.5 316.5z\" /> </svg>"
},
/* Callbaks */
onMapLock: function() {},
onMapUnlock: function() {},
/* Print Log Messges */
printLog: false
};
Build From Source
To build from source you can use the packaje.json
file to install all "dev dependencies". We use Gulp and some node-modules
.
- Download or Clone this repo with a
git
client. - Install
node.js
. - Install gulp globally
npm install gulp -g
. - Do a
npm update --save-dev
in your terminal. - Edit your
googlemaps-scrollprevent.coffee
source file. - Do a
gulp coffee
orgulp watch
task in your terminal. - Use the newly compiled
googlemaps-scrollprevent.min.js
file in the./dist/
folder. - If you make cool improvements please fork and contribute.
License
The MIT License (MIT)
Copyright (c) 2016 Emiliano Díaz
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.