2017-02-09 5 views
0

Я все еще изучаю jQuery, и у меня есть небольшая проблема с разработкой того, что мне нужно сделать ... Мне нужно иметь маркеры на карте, и когда вы просматриваете их, они показывают информацию , Итак, у меня есть две проблемы, о которых я не уверен:Показать div от родителя при наведении курсора с jQuery

1) Как вы можете снова скрыть div при отпускании пари? 2) Как написать код, чтобы div родителя был единственным, который открывается, а не все из них?

$(function() { 
 
$(".block").hover(function(){ 
 
\t $('.popup').show(); 
 
\t }); 
 
});
\t .wrapper { 
 
\t \t width: 800px; 
 
\t \t height: 600px; 
 
\t \t position: relative; 
 
\t \t background-color: #efefef; 
 
\t \t margin: 0 auto; 
 
\t } 
 

 
\t .block { 
 
\t \t width: 40px; 
 
\t \t height: 40px; 
 
\t \t border-radius: 20px; 
 
\t \t background-color: #8E2729; 
 
\t \t border:2px solid #fff; 
 
\t \t cursor: pointer; 
 
\t } 
 
\t 
 
\t .block:hover{ 
 
    \t \t background-color: #5151B7; 
 
\t } 
 
\t 
 
\t .block-1 { 
 
\t \t position: absolute; 
 
\t \t top: 250px; 
 
\t \t left: 130px; 
 
\t } 
 
\t 
 
\t .block-2 { 
 
\t \t position: absolute; 
 
\t \t top: 60px; 
 
\t \t left: 500px; 
 
\t } 
 
\t 
 
\t .block-3 { 
 
\t \t position: absolute; 
 
\t \t top: 40px; 
 
\t \t left: 100px; 
 
\t } 
 
\t 
 
\t .popup { 
 
\t \t box-sizing: border-box; 
 
\t \t padding: 30px; 
 
\t \t background-color: #4E4E4E; 
 
\t \t display: none; 
 
\t \t width: 300px; 
 
\t \t position: absolute; 
 
\t \t top: 50px; 
 
\t \t left: -80px; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrapper"> 
 

 
\t <div class="block block-1"> 
 
\t \t <div class="popup">This is some popup text for block 1</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-2"> 
 
\t \t <div class="popup">This is some popup text for block 2</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-3"> 
 
\t \t <div class="popup">This is some popup text for block 3</div> 
 
\t </div> 
 

 
</div>

+0

Я просто выработал шкурку на выпуске - изменение 'шоу' в 'тумблер'. Я предполагаю, что так? Поэтому его просто сделать это только открыть ребенка div, что мне нужна помощь с ... – DaveP19

ответ

1

Использование childern() method.The children() метод возвращает все прямые дети выбранного element.Define две функции для hover() method.The hover() метод задает две функции для запуска при наведении указателя мыши выбранные элементы. Этот метод запускает как события mouseenter, так и mouseleave.

$(function() { 
$(".block").hover(function(){ 
    $(this).children('.popup').show(); 
    },function(){ 
    $(this).children('.popup').hide(); 
    }); 
}); 

$(function() { 
 
$(".block").hover(function(){ 
 
\t $(this).children('.popup').show(); 
 
\t },function(){ 
 
    $(this).children('.popup').hide(); 
 
    }); 
 
});
.wrapper { 
 
\t \t width: 800px; 
 
\t \t height: 600px; 
 
\t \t position: relative; 
 
\t \t background-color: #efefef; 
 
\t \t margin: 0 auto; 
 
\t } 
 

 
\t .block { 
 
\t \t width: 40px; 
 
\t \t height: 40px; 
 
\t \t border-radius: 20px; 
 
\t \t background-color: #8E2729; 
 
\t \t border:2px solid #fff; 
 
\t \t cursor: pointer; 
 
\t } 
 
\t 
 
\t .block:hover{ 
 
    \t \t background-color: #5151B7; 
 
\t } 
 
\t 
 
\t .block-1 { 
 
\t \t position: absolute; 
 
\t \t top: 250px; 
 
\t \t left: 130px; 
 
\t } 
 
\t 
 
\t .block-2 { 
 
\t \t position: absolute; 
 
\t \t top: 60px; 
 
\t \t left: 500px; 
 
\t } 
 
\t 
 
\t .block-3 { 
 
\t \t position: absolute; 
 
\t \t top: 40px; 
 
\t \t left: 100px; 
 
\t } 
 
\t 
 
\t .popup { 
 
\t \t box-sizing: border-box; 
 
\t \t padding: 30px; 
 
\t \t background-color: #4E4E4E; 
 
\t \t display: none; 
 
\t \t width: 300px; 
 
\t \t position: absolute; 
 
\t \t top: 50px; 
 
\t \t left: -80px; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrapper"> 
 

 
\t <div class="block block-1"> 
 
\t \t <div class="popup">This is some popup text for block 1</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-2"> 
 
\t \t <div class="popup">This is some popup text for block 2</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-3"> 
 
\t \t <div class="popup">This is some popup text for block 3</div> 
 
\t </div> 
 

 
</div>

См скрипка здесь JS Fiddle

+0

Perfect. Благодарю. – DaveP19

0

Используй toggle, а затем show и find всплывающего окна в ассоциированных делах.

$(function() { 
 
$(".block").hover(function(){ 
 
\t $(this).find('.popup').toggle(); 
 
\t }); 
 
});
\t .wrapper { 
 
\t \t width: 800px; 
 
\t \t height: 600px; 
 
\t \t position: relative; 
 
\t \t background-color: #efefef; 
 
\t \t margin: 0 auto; 
 
\t } 
 

 
\t .block { 
 
\t \t width: 40px; 
 
\t \t height: 40px; 
 
\t \t border-radius: 20px; 
 
\t \t background-color: #8E2729; 
 
\t \t border:2px solid #fff; 
 
\t \t cursor: pointer; 
 
\t } 
 
\t 
 
\t .block:hover{ 
 
    \t \t background-color: #5151B7; 
 
\t } 
 
\t 
 
\t .block-1 { 
 
\t \t position: absolute; 
 
\t \t top: 250px; 
 
\t \t left: 130px; 
 
\t } 
 
\t 
 
\t .block-2 { 
 
\t \t position: absolute; 
 
\t \t top: 60px; 
 
\t \t left: 500px; 
 
\t } 
 
\t 
 
\t .block-3 { 
 
\t \t position: absolute; 
 
\t \t top: 40px; 
 
\t \t left: 100px; 
 
\t } 
 
\t 
 
\t .popup { 
 
\t \t box-sizing: border-box; 
 
\t \t padding: 30px; 
 
\t \t background-color: #4E4E4E; 
 
\t \t display: none; 
 
\t \t width: 300px; 
 
\t \t position: absolute; 
 
\t \t top: 50px; 
 
\t \t left: -80px; 
 
\t }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrapper"> 
 

 
\t <div class="block block-1"> 
 
\t \t <div class="popup">This is some popup text for block 1</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-2"> 
 
\t \t <div class="popup">This is some popup text for block 2</div> 
 
\t </div> 
 
\t 
 
\t <div class="block block-3"> 
 
\t \t <div class="popup">This is some popup text for block 3</div> 
 
\t </div> 
 

 
</div>

+0

См. Обновление с помощью find. –