Смена значка маркера при наведении мышкой

Open in CodeSandbox

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
            import {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';
            
            window.map = null;
            
            main();
            
            async function main() {
                // Waiting for all api elements to be loaded
                await ymaps3.ready;
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = ymaps3;
                const {YMapDefaultMarker} = await ymaps3.import('@yandex/ymaps3-default-ui-theme');
            
                // Initialize the map
                map = new YMap(
                    // Pass the link to the HTMLElement of the container
                    document.getElementById('app'),
                    // Pass the map initialization parameters
                    {location: LOCATION, showScaleInCopyrights: true},
                    [
                        // Add a map scheme layer
                        new YMapDefaultSchemeLayer({}),
                        // Add a layer of geo objects to display the markers
                        new YMapDefaultFeaturesLayer({})
                    ]
                );
                let isActive = false;
            
                const marker = new YMapDefaultMarker({
                    ...MARKER_PROPS_DEFAULT,
                    onMouseEnter: () => {
                        marker.update({color: isActive ? 'red' : 'darkblue'});
                    },
                    onMouseLeave: () => {
                        marker.update(!isActive ? MARKER_PROPS_DEFAULT : MARKER_PROPS_ACTIVE);
                    },
                    onClick: () => {
                        isActive = true;
                        marker.update(MARKER_PROPS_ACTIVE);
                    }
                });
            
                const listenerMap = new YMapListener({
                    layer: 'any',
                    onClick: (object) => {
                        isActive = false;
                        if (object?.type !== 'marker') marker.update(MARKER_PROPS_DEFAULT);
                    }
                });
            
                map.addChild(listenerMap);
                map.addChild(marker);
            }
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="react, typescript" type="text/babel">
            import {MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, MARKER_PROPS_ACTIVE, LOCATION} from '../variables';
            import {DomEventHandler} from '@yandex/ymaps3-types';
            
            window.map = null;
            
            main();
            
            async function main() {
                // For each object in the JS API, there is a React counterpart
                // To use the React version of the API, include the module @yandex/ymaps3-reactify
                const [ymaps3React] = await Promise.all([ymaps3.import('@yandex/ymaps3-reactify'), ymaps3.ready]);
                const reactify = ymaps3React.reactify.bindTo(React, ReactDOM);
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = reactify.module(ymaps3);
                const {YMapDefaultMarker} = await reactify.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
            
                const {useCallback, useMemo} = React;
            
                function App() {
                    const [pointOnMarker, setPointOnMarker] = React.useState(false);
                    const [markerActive, setMarkerActive] = React.useState(false);
            
                    const onMouseEnter: DomEventHandler = useCallback(() => {
                        setPointOnMarker(true);
                    }, []);
            
                    const onMouseLeave: DomEventHandler = useCallback(() => {
                        setPointOnMarker(false);
                    }, []);
            
                    const onMouseClick: DomEventHandler = useCallback((object) => {
                        if (object && object.type === 'marker') {
                            setMarkerActive(true);
                        } else {
                            setMarkerActive(false);
                        }
                    }, []);
            
                    const markerProps = useMemo(
                        () => (markerActive ? MARKER_PROPS_ACTIVE : pointOnMarker ? MARKER_PROPS_HOVER : MARKER_PROPS_DEFAULT),
                        [markerActive, pointOnMarker]
                    );
            
                    return (
                        // Initialize the map and pass initialization parameters
                        <YMap location={LOCATION} showScaleInCopyrights={true} ref={(x) => (map = x)}>
                            {/* Add a map scheme layer */}
                            <YMapDefaultSchemeLayer />
                            {/* Add a layer of geo objects to display the markers */}
                            <YMapDefaultFeaturesLayer />
            
                            <YMapDefaultMarker {...markerProps} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} onClick={onMouseClick} />
            
                            <YMapListener onClick={onMouseClick} layer="any" />
                        </YMap>
                    );
                }
            
                ReactDOM.render(
                    <React.StrictMode>
                        <App />
                    </React.StrictMode>,
                    document.getElementById('app')
                );
            }
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
        <script crossorigin src="https://cdn.jsdelivr.net/npm/vue@3/dist/vue.global.js"></script>
        <script crossorigin src="https://cdn.jsdelivr.net/npm/@babel/standalone@7/babel.min.js"></script>
        <!-- To make the map appear, you must add your apikey -->
        <script src="https://api-maps.yandex.ru/v3/?apikey=<YOUR_APIKEY>&lang=en_US" type="text/javascript"></script>

        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="../variables.ts"
        ></script>
        <script
            data-plugins="transform-modules-umd"
            data-presets="typescript"
            type="text/babel"
            src="./common.ts"
        ></script>
        <script data-plugins="transform-modules-umd" data-presets="typescript" type="text/babel">
            import {MARKER_PROPS_ACTIVE, MARKER_PROPS_DEFAULT, MARKER_PROPS_HOVER, LOCATION} from '../variables';
            import {DomEventHandler} from '@yandex/ymaps3-types';
            
            window.map = null;
            
            async function main() {
                const [ymaps3Vue] = await Promise.all([ymaps3.import('@yandex/ymaps3-vuefy'), ymaps3.ready]);
                const vuefy = ymaps3Vue.vuefy.bindTo(Vue);
                const {YMap, YMapDefaultSchemeLayer, YMapDefaultFeaturesLayer, YMapListener} = vuefy.module(ymaps3);
                const {YMapDefaultMarker} = await vuefy.module(await ymaps3.import('@yandex/ymaps3-default-ui-theme'));
            
                const App = Vue.createApp({
                    components: {
                        YMap,
                        YMapDefaultSchemeLayer,
                        YMapDefaultFeaturesLayer,
                        YMapListener,
                        YMapDefaultMarker
                    },
                    setup() {
                        const map = Vue.ref(null);
                        const pointOnMarker = Vue.ref(false);
                        const markerActive = Vue.ref(false);
                        const refMap = (ref) => {
                            window.map = ref?.entity;
                        };
            
                        const layerName = `${ymaps3.YMapDefaultFeaturesLayer.defaultProps.source}:markers`;
            
                        const onMouseEnter: DomEventHandler = () => {
                            pointOnMarker.value = true;
                        };
            
                        const onMouseLeave: DomEventHandler = () => {
                            pointOnMarker.value = false;
                        };
            
                        const onMouseClick: DomEventHandler = (object) => {
                            markerActive.value = object && object.type === 'marker';
                        };
            
                        return {
                            map,
                            LOCATION,
                            refMap,
                            pointOnMarker,
                            markerActive,
                            onMouseEnter,
                            onMouseLeave,
                            onMouseClick,
                            layerName
                        };
                    },
                    computed: {
                        markerProps() {
                            return this.markerActive
                                ? MARKER_PROPS_ACTIVE
                                : this.pointOnMarker
                                ? MARKER_PROPS_HOVER
                                : MARKER_PROPS_DEFAULT;
                        }
                    },
                    template: `
                  <YMap :location="LOCATION" :showScaleInCopyrights="true" :ref="refMap">
                    <YMapDefaultSchemeLayer/>
            
                    <YMapDefaultFeaturesLayer/>
            
                    <YMapDefaultMarker
                      v-bind="markerProps"
                      :onMouseEnter="onMouseEnter"
                      :onMouseLeave="onMouseLeave"
                      :onClick="onMouseClick"
                    />
            
                    <YMapListener
                      :onClick="onMouseClick"
                      layer="any"
                    />
                  </YMap>
                `
                });
            
                App.mount('#app');
            }
            
            main();
        </script>

        <style> html, body, #app { width: 100%; height: 100%; margin: 0; padding: 0; font-family: Arial, Helvetica, sans-serif; } .toolbar { position: absolute; z-index: 1000; top: 0; left: 0; display: flex; align-items: center; padding: 16px; } .toolbar a { padding: 16px; }  </style>
        <link rel="stylesheet" href="./common.css" />
        <link rel="stylesheet" href="../variables.css" />
    </head>
    <body>
        <div id="app"></div>
    </body>
</html>
import type {LngLat, YMapLocationRequest} from '@yandex/ymaps3-types';
import type {YMapDefaultMarkerProps} from '@yandex/ymaps3-default-ui-theme';

type MarkerProps = Omit<YMapDefaultMarkerProps, 'popup'>;

export const MARKER_PROPS_DEFAULT: MarkerProps = {
    iconName: 'airport',
    color: 'red',
    size: 'small',
    coordinates: [37.415, 55.9599]
};

export const MARKER_PROPS_HOVER: MarkerProps = {
    iconName: 'airport',
    color: 'darkblue',
    size: 'small',
    coordinates: [37.415, 55.9599]
};

export const MARKER_PROPS_ACTIVE: MarkerProps = {
    iconName: 'airport',
    size: 'normal',
    color: 'red',
    coordinates: [37.415, 55.9599]
};

export const LOCATION: YMapLocationRequest = {
    center: [37.415, 55.9599], // starting position [lng, lat]
    zoom: 10 // starting zoom
};
ymaps3.ready.then(() => {
    ymaps3.import.registerCdn('https://cdn.jsdelivr.net/npm/{package}', '@yandex/ymaps3-default-ui-theme@0.0');
});
.marker {
    cursor: pointer;

    width: 25px;
    height: 25px;
    border-radius: 50%;

    position: relative;
    transform: translate(-50%, -50%);
}