M
M
ML2017-03-28 16:57:01
JavaScript
ML, 2017-03-28 16:57:01

Why doesn't it override the default reaction to the event?

When double-clicking on the layer, the zoom remains, although it costs event.preventDefault()
And with the same handler, but clicking on the map it works, why so?

<!DOCTYPE html>

<html>

<head>
    <title>Map</title>
    <meta charset="utf-8">
    <script src="//api-maps.yandex.ru/2.0-stable/?load=package.standard,package.geoObjects,package.regions&lang=ru-RU" type="text/javascript"></script>
    <style type="text/css">
        html, body, #container {
            width: 100%;
            height: 100%;
        }
    </style>

  <script src="regions.js" type="text/javascript"></script>
</head>

<body>
    <div id="container"></div>
</body>

</html>

var lcln = {},
    fmap = function( geoMap, lang, color ) {
        ymaps.regions.load( lang, {
            lang: 'ru',
            quality: 3
        } ).then( function ( result ) {
            var lastCollection = result.geoObjects;
            lcln[ lang ] = lastCollection;
            lastCollection.options.set( {
                zIndex: 1,
                zIndexHover: 2,
                draggable: false,
                fillColor: color,
                strokeColor: color
            } );
            
            console.log( lastCollection.events );
            lastCollection.events.add( 'dblclick', function ( event ) {
                 event.preventDefault() // не отменяет зум по слою
            } );

            geoMap.geoObjects.add( lastCollection );
        }, function () {

        } );
    };

ymaps.ready( function () {
    geoMap = new ymaps.Map( 'container', {
        center:  [ 65, 104 ],
        type: "yandex#map",
        zoom: 4
    } );
    
    geoMap.events.add( 'dblclick', function ( event ) {
        event.preventDefault(); // а здесь отменяет зум по карте
    } );
    geoMap.behaviors.enable( 'scrollZoom' ); 
    
    var lng = 'ru',
        level = 3;
    
    fmap( geoMap, 'RU', 'rgba(13, 91, 58, 0.2)' );
    fmap( geoMap, 'UA', 'rgba(203, 37, 37, 0.2)' );
} );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dummyman, 2017-03-28
@dummyman

event.preventDefalult()
is correct
event.preventDefault()

A
Anatoly, 2017-03-28
@Skit25

It is necessary not to cancel the click, but to cancel the zoom.

lastCollection.events.add( 'dblclick', function ( event ) {
                 zoom.nenado(); // логика будет другая, но суть в отмене зума при клике, а не клика при клике
            } );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question