2017-02-03 19 views
0

私は現在、マーカーに固有のテキストを表示するウェブサイトを作成しようとしています。jQueryマーカーを追加

ユーザーがマーカーをクリックすると、テキストが表示されます。

私はいくつかのコードをオンラインでコピーし、それを使いこなそうとしています。残念ながら、私は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を(私が苦労してる部分):

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 ;} 
} 

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は(私はここで何の問題もないと思います)追加または削除するにはjqueryの機能toggleClassを使用した期間

2を追加することで、クラスpopuptextをつかむために$captionためのセレクタを修正)

1):あなたは2通りの方法でごshowBeaconInfo機能を更新する必要が3210

答えて

1

:popuptext上のクラス "show"は、CSSはいずれかの例については visibility: hiddenまたは visibility: visible

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>

+0

。それは完全に動作します!ありがとうございました!!!! – Jay

関連する問題