Skip to content

diazemiliano-zz/googlemaps-scrollprevent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

  1. How to disable mouse scroll-wheel scaling with Google Maps API
  2. Disable mouse scroll wheel zoom on embedded Google Maps
  3. Prevent a Google Maps iframe from capturing the mouse's scrolling wheel behavior

Details

Weight
License MIT
Website Link
Donate

Minimun Requeriments

jQuery Core 1.12.4

Table of contents

Examples

For usage examples check the live demo.

Usage as jQuery plugin

  1. 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>
  2. 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();
  3. 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>
  4. 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

  1. 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.

  1. Download or Clone this repo with a git client.
  2. Install node.js.
  3. Install gulp globally npm install gulp -g.
  4. Do a npm update --save-dev in your terminal.
  5. Edit your googlemaps-scrollprevent.coffee source file.
  6. Do a gulp coffee or gulp watch task in your terminal.
  7. Use the newly compiled googlemaps-scrollprevent.min.js file in the ./dist/ folder.
  8. 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.

About

🔒 googlemaps-scrollprevent avoid unwanted map interactions with the Google Maps Iframe.

Resources

Stars

Watchers

Forks

Packages

No packages published