---
metadata:
  - name: generator
    content: Diplodoc Platform v5.39.1
alternate:
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/en/v2-1/examples/cases/balloon_out_of_map.md
  - https://yandex.com.tr/dev/jsapi-v2-1/doc/ru/v2-1/examples/cases/balloon_out_of_map.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com.tr/dev/jsapi-v2-1/doc/en/llms.txt

# Balloon that goes off the map

<iframe id="LIVE-EXAMPLE" style="width:100%;height: 400px;border-radius: 8px;border: 1px solid rgba(92, 94, 102, 0.14);" frameBorder="0" src="https://yastatic.net/s3/front-maps-static/maps-front-jsapi-v2-1/examples/2/out/s3-cases/en/balloon_out_of_map/index.html" allow="fullscreen"></iframe>

<a target="_blank" rel="noopener noreferrer" style="display: inline-block;margin-top: 10px;cursor: pointer;border-radius: 4px;padding: 9px 12px;background: #151515;color: white;text-decoration: none;font-weight: 500" href="https://codesandbox.io/p/sandbox/zv7zfd?file=index.html">Open in CodeSandbox</a>

<div id="references">
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/ready">ready</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/Map">Map</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.Balloon">map.Balloon</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.pane.Manager">map.pane.Manager</a>,
    <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/util.bounds">util.bounds</a>
</div>
<p>
    The balloon can be displayed in a <a href="https://yandex.ru/dev/jsapi-v2-1/doc/en/v2-1/ref/reference/map.pane.Manager">pane</a> with boundaries that go off the map. This is useful when the map is small, but the balloon needs to display a large amount of information.
</p>
<p>
    In this example, in addition to using options for displaying a balloon in an external pane, tracking is enabled for the current position of the anchor point, which when moved outside the visible area of the map, the balloon once again fits inside the internal pane.
</p>

{% list tabs %}

-   index.html

    ```html
    <!DOCTYPE html>
    <html>
        <head>
            <title>Examples. A balloon that goes off the map</title>
            <meta
                http-equiv="Content-Type"
                content="text/html; charset=UTF-8"
            />
            <!--
            Set your own API-key. Testing key is not valid for other web-sites and services.
            Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
        -->
            
            
            
        </head>

        <body>
            <div id="map"></div>
        </body>
    </html>
    ```

-   balloon_out_of_map.js

    ```js
    var myMap;

    ymaps.ready(function () {
        myMap = new ymaps.Map(
            "map",
            {
                zoom: 4,
                center: [52.6, 30.08],
                controls: [],
            },
            {
                searchControlProvider: "yandex#search",
            }
        );
        var myPlacemark = new ymaps.Placemark(
            [55.76, 37.64],
            {
                balloonContent: "I went off the map",
            },
            {
                balloonPanelMaxMapArea: 0,
            }
        );
        myMap.geoObjects.add(myPlacemark);

        observeEvents(myMap);

        myPlacemark.balloon.open();
    });

    function observeEvents(map) {
        var mapEventsGroup;
        map.geoObjects.each(function (geoObject) {
            geoObject.balloon.events
                // When opening a balloon, begin to listen for a change to the center of the map.
                .add("open", function (e1) {
                    var placemark = e1.get("target");
                    // Calling a function in two cases:
                    mapEventsGroup = map.events
                        .group()
                        // 1) in the beginning of the movement (if the balloon is in an external container),
                        .add("actiontick", function (e2) {
                            if (
                                placemark.options.get("balloonPane") ==
                                "outerBalloon"
                            ) {
                                setBalloonPane(map, placemark, e2.get("tick"));
                            }
                        })
                        // 2) at the end of the movement (if the balloon is in the internal container).
                        .add("actiontickcomplete", function (e2) {
                            if (
                                placemark.options.get("balloonPane") !=
                                "outerBalloon"
                            ) {
                                setBalloonPane(map, placemark, e2.get("tick"));
                            }
                        });
                    // Calling the function immediately after opening.
                    setBalloonPane(map, placemark);
                })
                // When closing a balloon, we remove listeners.
                .add("close", function () {
                    mapEventsGroup.removeAll();
                });
        });
    }

    function setBalloonPane(map, placemark, mapData) {
        mapData = mapData || {
            globalPixelCenter: map.getGlobalPixelCenter(),
            zoom: map.getZoom(),
        };

        var mapSize = map.container.getSize(),
            mapBounds = [
                [
                    mapData.globalPixelCenter[0] - mapSize[0] / 2,
                    mapData.globalPixelCenter[1] - mapSize[1] / 2,
                ],
                [
                    mapData.globalPixelCenter[0] + mapSize[0] / 2,
                    mapData.globalPixelCenter[1] + mapSize[1] / 2,
                ],
            ],
            balloonPosition = placemark.balloon.getPosition(),
            // Used when changing the zoom.
            zoomFactor = Math.pow(2, mapData.zoom - map.getZoom()),
            // Determining whether the anchor point of a balloon is in the visible area of the map.
            pointInBounds = ymaps.util.pixelBounds.containsPoint(mapBounds, [
                balloonPosition[0] * zoomFactor,
                balloonPosition[1] * zoomFactor,
            ]),
            isInOutersPane =
                placemark.options.get("balloonPane") == "outerBalloon";

        // If the anchor does not fall within the visible area of the map, we move the balloon into the inner container
        if (!pointInBounds && isInOutersPane) {
            placemark.options.set({
                balloonPane: "balloon",
                balloonShadowPane: "shadows",
            });
            // and the reverse.
        } else if (pointInBounds && !isInOutersPane) {
            placemark.options.set({
                balloonPane: "outerBalloon",
                balloonShadowPane: "outerBalloon",
            });
        }
    }
    ```

{% endlist %}
