javascript - jQuery 添加标记

标签 javascript jquery html css

我目前正在尝试制作一个显示带有自己特定文本的标记的网站。

当用户点击标记时,它会显示文本。

我已经在线复制了一些代码并试图弄乱它。不幸的是,我不太熟悉 javascript。我想知道是否有人能够提供帮助。

这是我目前所拥有的:

HTML(我认为这里没有任何问题):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>MarkerWeb</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  </head>
  <body>
    <div id="container">
      <div id="image-wrapper" data-captions='{"coords": [{"top":250,"left":200,"text":"iMac 1"},{"top":250,"left":540,"text":"iMac 2"}]}'>
        <!-- layout.jpg is just a picture of a layout/floorplan -->
          <img src="layout.jpg" alt=""/>
      </div>
  </div>
  </body>
  <script type="text/javascript" src="script.js"></script>
</html>

Javascript(我遇到困难的部分):

var Markers = {
    fn: {
        addMarkers: function() {
            var target = $('#image-wrapper');
            var data = target.attr('data-captions');
            var captions = $.parseJSON(data);
            var coords = captions.coords;

            for (var i = 0; i < coords.length; i++) {
                var obj = coords[i];
                var top = obj.top;
                var left = obj.left;
                var text = obj.text;

                $('<div class="marker"><div class="pin"><span class="popuptext" id="myPopup">' + text + '</span></div><div class="pin-effect"></div></div>').css({
                    top: top,
                    left: left
                }).appendTo(target);
            }
        },


        //This is the part I'm having trouble with.
        showBeaconInfo: function() {
            $('body').on('click', '.marker', function() {
                var $marker = $(this),
                    $caption = $('popuptext', $marker);
                    if ($caption.is(':hidden')) {
                        $caption.show();

                    } else {
                        $caption.toggle("show");

                    }


            });

        }
    },

    init: function() {
        this.fn.addMarkers();
        this.fn.showBeaconInfo();
    }
};

function clickBeacon() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
}

$(function() {
    Markers.init();

});

CSS(我认为这里没有任何问题):

body {
  background: #e6e6e6;
}
#image-wrapper {
    width: 800px;
    height: 760px;
    position: relative;
    margin: 2em auto;
    background: #f6f6f6;
    border: 2px solid #ddd;
}

#image-wrapper img {
    display: block;
    margin: 25px auto;
}
.map-bg {
    background: url(images/map-bg.jpg) no-repeat;
    background-position: 0px 0px;
    background-size: auto;
    width: 100%;
    height: 440px; /*adjust to the height of your image*/
    position: relative;
}

.marker {
    width: 100px;
    height: 100px;
    position: absolute;
    /*top: 130px; /*positions our marker*/
    /*left: 200px; /*positions our marker*/
    display: block;
}

.pin {
    width: 20px;
    height: 20px;
    position: relative;
    top: 38px;
    left: 38px;
    background: rgba(5, 124, 255, 1);
    border: 2px solid #FFF;
    border-radius: 50%;
    z-index: 1000;
    cursor: pointer;

    display: inline-block;
}

.pin-effect {
    width: 100px;
    height: 100px;
    position: absolute;
    top: 0;
    display: block;
    background: rgba(5, 124, 255, 0.6);
    border-radius: 50%;
    opacity: 0;
    animation: pulsate 2400ms ease-out infinite;
}

@keyframes pulsate {
    0% {
    transform: scale(0.1);
    opacity: 0;
    }
    50% {
    opacity: 1;
    }
    100% {
    transform: scale(1.2);
    opacity: 0;
    }
}









/* The actual popup (appears on top) */
.pin .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 8px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    left: 50%;
    margin-left: -80px;
}

/* Popup arrow */
.pin .popuptext::after {
    content: "";
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -5px;
    border-width: 5px;
    border-style: solid;
    border-color: #555 transparent transparent transparent;
}

/* Toggle this class when clicking on the popup container (hide and show the popup) */
.pin .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;}
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}

最佳答案

您需要通过两种方式更新您的showBeaconInfo 函数:

1) 通过添加句点修复 $caption 的选择器以获取类 popuptext

2) 使用 jquery 函数 toggleClass 在弹出文本上添加或删除类 "show",css 设置为使用 visibility: hidden 可见性:可见

showBeaconInfo: function() {
  $('body').on('click', '.marker', function() {
    var $marker = $(this);
    var $caption = $('.popuptext', $marker); //needs period to indicate class
    $caption.toggleClass('show'); //adding the class "show" will display the text
  });
}

例如:

var Markers = {
  fn: {
    addMarkers: function() {
      var target = $('#image-wrapper');
      var data = target.attr('data-captions');
      var captions = $.parseJSON(data);
      var coords = captions.coords;

      for (var i = 0; i < coords.length; i++) {
        var obj = coords[i];
        var top = obj.top;
        var left = obj.left;
        var text = obj.text;

        $('<div class="marker"><div class="pin"><span class="popuptext" id="myPopup">' + text + '</span></div><div class="pin-effect"></div></div>').css({
          top: top,
          left: left
        }).appendTo(target);
      }
    },


    //This is the part I'm having trouble with.
    showBeaconInfo: function() {
      $('body').on('click', '.marker', function() {
        var $marker = $(this);
        var $caption = $('.popuptext', $marker); //needs period to indicate class
        $caption.toggleClass('show'); //adding the class "show" will display the text
      });
    }
  },

  init: function() {
    this.fn.addMarkers();
    this.fn.showBeaconInfo();
  }
};

function clickBeacon() {
  var popup = document.getElementById("myPopup");
  popup.classList.toggle("show");
}

$(function() {
  Markers.init();

});
body {
  background: #e6e6e6;
}
#image-wrapper {
  width: 800px;
  height: 760px;
  position: relative;
  margin: 2em auto;
  background: #f6f6f6;
  border: 2px solid #ddd;
}
#image-wrapper img {
  display: block;
  margin: 25px auto;
}
.map-bg {
  background: url(images/map-bg.jpg) no-repeat;
  background-position: 0px 0px;
  background-size: auto;
  width: 100%;
  height: 440px;
  /*adjust to the height of your image*/
  position: relative;
}
.marker {
  width: 100px;
  height: 100px;
  position: absolute;
  /*top: 130px; /*positions our marker*/
  /*left: 200px; /*positions our marker*/
  display: block;
}
.pin {
  width: 20px;
  height: 20px;
  position: relative;
  top: 38px;
  left: 38px;
  background: rgba(5, 124, 255, 1);
  border: 2px solid #FFF;
  border-radius: 50%;
  z-index: 1000;
  cursor: pointer;
  display: inline-block;
}
.pin-effect {
  width: 100px;
  height: 100px;
  position: absolute;
  top: 0;
  display: block;
  background: rgba(5, 124, 255, 0.6);
  border-radius: 50%;
  opacity: 0;
  animation: pulsate 2400ms ease-out infinite;
}
@keyframes pulsate {
  0% {
    transform: scale(0.1);
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    transform: scale(1.2);
    opacity: 0;
  }
}
/* The actual popup (appears on top) */

.pin .popuptext {
  visibility: hidden;
  width: 160px;
  background-color: #555;
  color: #fff;
  text-align: center;
  border-radius: 6px;
  padding: 8px 0;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -80px;
}
/* Popup arrow */

.pin .popuptext::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: #555 transparent transparent transparent;
}
/* Toggle this class when clicking on the popup container (hide and show the popup) */

.pin .show {
  visibility: visible;
  -webkit-animation: fadeIn 1s;
  animation: fadeIn 1s
}
/* Add animation (fade in the popup) */

@-webkit-keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div id="image-wrapper" data-captions='{"coords": [{"top":250,"left":200,"text":"iMac 1"},{"top":250,"left":540,"text":"iMac 2"}]}'>
    <!-- layout.jpg is just a picture of a layout/floorplan -->
    <img src="layout.jpg" alt="" />
  </div>
</div>

关于javascript - jQuery 添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42014944/

相关文章:

javascript - 单击列表元素以获取警报

javascript - 创建了一个 Windows 服务来运行我的 Node 应用程序,但服务在启动几秒钟后停止运行

javascript - 无法在函数中访问jquery。 "Variable Scope"问题?

jquery - 访问递归添加字段的 jquery 类名

javascript - 尝试对齐大小不同的div的位置,以使它们之间没有任何空格

javascript - 需要帮助使我的 HTML 和 CSS 代码更有效地实现导航效果

Javascript 开关不起作用

jquery - 如何使用 jQuery Validator 获取所有无效元素?

javascript - 简化糟糕的 jQuery

javascript - JavaScript 中的 360 度旋转 View