Создание карт изображений в HTML: примеры синтаксиса и кода

Термин «синтаксис карты изображений» относится к синтаксису или коду, используемому для создания карт изображений в HTML. Карта-изображение — это изображение с интерактивными областями, которые могут служить ссылками на различные пункты назначения. Вот несколько методов создания карт изображений в HTML, а также примеры кода:

  1. Элементы HTML

    и

    :

    <img src="image.jpg" alt="Image" usemap="#map">
    <map name="map">
    <area shape="rect" coords="0,0,100,100" href="destination1.html" alt="Area 1">
    <area shape="circle" coords="150,150,50" href="destination2.html" alt="Area 2">
    <area shape="poly" coords="200,200,250,250,200,300" href="destination3.html" alt="Area 3">
    </map>
  2. Сопоставление фонового изображения CSS:

    <div class="image-map">
    <a href="destination1.html" class="area1"></a>
    <a href="destination2.html" class="area2"></a>
    <a href="destination3.html" class="area3"></a>
    </div>
    <style>
    .image-map {
    background-image: url('image.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    width: 500px;
    height: 500px;
    }
    .area1 {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 100px;
    }
    .area2 {
    position: absolute;
    top: 150px;
    left: 150px;
    width: 100px;
    height: 100px;
    }
    .area3 {
    position: absolute;
    top: 200px;
    left: 200px;
    width: 100px;
    height: 100px;
    }
    </style>
  3. Сопоставление изображений JavaScript:

    <img src="image.jpg" alt="Image" id="image">
    <script>
    var image = document.getElementById('image');
    image.addEventListener('click', function(event) {
    var x = event.pageX - image.offsetLeft;
    var y = event.pageY - image.offsetTop;
    
    if (x >= 0 && x <= 100 && y >= 0 && y <= 100) {
    window.location.href = 'destination1.html';
    } else if (x >= 150 && x <= 200 && y >= 150 && y <= 200) {
    window.location.href = 'destination2.html';
    } else if (x >= 200 && x <= 300 && y >= 200 && y <= 300) {
    window.location.href = 'destination3.html';
    }
    });
    </script>