2016-08-03 33 views
1

Also habe ich eine Liste eingerichtet, so dass wenn ich über die verschiedenen Elemente schwebe, wird das Bild rechts von den Elementen geändert. Ich möchte Text mit dem Bild gehen, so dass ich das in einem div unten mögen würde. Gibt es einen Weg mit CSS, um es so zu machen, wenn ich über ein Element in der Liste schwebe, ändert sich der Text in der div? Normalerweise versuche ich, diese Dinge selbst herauszufinden, aber ich habe eine Weile gesucht und kann keine Lösung finden.Ändere den Text in einem Div, wenn er über einem Listenelement schwebte

HTML:

<div class="relative"> 
<ul> 
    <li> 
     <a href="#">Arms</a> 
     <img src="img/arms.jpg" alt=""> 

      <div class="absolute">TEXT TEST 1</div> 

    </li> 
    <li> 
     <a href="#">Back</a> 
     <img src="img/back.jpg" alt=""> 
     <div class="absolute">TEXT TEST 2</div> 
    </li> 
    <li> 
     <a href="#">Head</a> 
     <img src="img/head.jpg" alt=""> 
    </li> 
    <li> 
     <a href="#">Feet</a> 
     <img src="img/feet.jpg" alt=""> 
    </li> 
    <li> 
     <a href="#">Legs</a> 
     <img src="img/legs.jpg" alt=""> 
    </li> 
    <li> 
     <a href="#">Chair</a> 
     <img src="img/chair.jpg" alt=""> 
    </li> 
    <li> 
     <a href="#">Desk</a> 
     <img src="img/desk.jpg" alt=""> 
    </li> 
</ul> 
</div> 

und hier ist die relative CSS:

ul { 
    background-image: url('../img/bg.jpg'); 
    background-repeat: no-repeat; 
    background-origin: border-box; 
    background-position:right top; 
    border-top: 1px solid #444; 
    border-bottom: 1px solid #444; 
    border-right: 1px solid #444; 
    display: inline-block; 
    height: 700px; 
    position: relative; 
    width: 800px; 
    list-style:none; 
    padding-left:0; 
} 
li { 
    font: bold 16px/100px sans-serif; 
    height: 100px; 
} 
a { 
    border-right: 1px solid #444; 
    border-top: 1px solid #444; 
    border-left: 1px solid #444; 
    color: black; 
    display: block; 
    text-align: center; 
    text-decoration: none; 
    width: 99px;  
} 
a:hover { 
    background: gray; 
    color: #fff; 
} 
div.relative { 
    position: relative; 
    width: 700; 
    height: 900; 
} 

div.absolute { 
    position: absolute; 
    top: 700px; 
    width: 800px; 
    height: 100px; 
    border: 1px solid #444; 
} 
h3{ 
    margin-left:200px; 
    margin-top:500px; 
} 
img { 
    display: none; 
    height: 700; 
    position: absolute; 
    right: 0; 
    top: 0; 
    width: 600px; 
    background-color:red; 
} 
a:hover + img, 
img:hover { 
    display: block; 
} 

Antwort

0

Hier ist eine Lösung. Verwenden Sie das Ereignis li:hover zum Ausblenden und Anzeigen der entsprechenden div.

ul { 
 
    background-image: url('../img/bg.jpg'); 
 
    background-repeat: no-repeat; 
 
    background-origin: border-box; 
 
    background-position: right top; 
 
    border-top: 1px solid #444; 
 
    border-bottom: 1px solid #444; 
 
    border-right: 1px solid #444; 
 
    display: inline-block; 
 
    height: 700px; 
 
    position: relative; 
 
    width: 800px; 
 
    list-style: none; 
 
    padding-left: 0; 
 
} 
 
li { 
 
    font: bold 16px/100px sans-serif; 
 
    height: 100px; 
 
} 
 
a { 
 
    border-right: 1px solid #444; 
 
    border-top: 1px solid #444; 
 
    border-left: 1px solid #444; 
 
    color: black; 
 
    display: block; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    width: 99px; 
 
} 
 
a:hover { 
 
    background: gray; 
 
    color: #fff; 
 
} 
 
div.relative { 
 
    position: relative; 
 
    width: 700; 
 
    height: 900; 
 
} 
 
div.absolute { 
 
    position: absolute; 
 
    top: 700px; 
 
    width: 800px; 
 
    height: 100px; 
 
    border: 1px solid #444; 
 
} 
 
h3 { 
 
    margin-left: 200px; 
 
    margin-top: 500px; 
 
} 
 
img { 
 
    display: none; 
 
    height: 700; 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    width: 600px; 
 
    background-color: red; 
 
} 
 
a:hover + img, 
 
img:hover { 
 
    display: block; 
 
} 
 
.absolute { 
 
    display: none; 
 
} 
 
.relative li:hover div.absolute { 
 
    display: initial; 
 
}
<div class="relative"> 
 
    <ul> 
 
    <li> 
 
     <a href="#">Arms</a> 
 
     <img src="img/arms.jpg" alt=""> 
 

 
     <div class="absolute">TEXT TEST 1</div> 
 

 
    </li> 
 
    <li> 
 
     <a href="#">Back</a> 
 
     <img src="img/back.jpg" alt=""> 
 
     <div class="absolute">TEXT TEST 2</div> 
 
    </li> 
 
    <li> 
 
     <a href="#">Head</a> 
 
     <img src="img/head.jpg" alt=""> 
 
    </li> 
 
    <li> 
 
     <a href="#">Feet</a> 
 
     <img src="img/feet.jpg" alt=""> 
 
    </li> 
 
    <li> 
 
     <a href="#">Legs</a> 
 
     <img src="img/legs.jpg" alt=""> 
 
    </li> 
 
    <li> 
 
     <a href="#">Chair</a> 
 
     <img src="img/chair.jpg" alt=""> 
 
    </li> 
 
    <li> 
 
     <a href="#">Desk</a> 
 
     <img src="img/desk.jpg" alt=""> 
 
    </li> 
 
    </ul> 
 
</div>

0

ich ein .wrapper div verwendet haben, aber sie mehr Möglichkeiten haben, wenn Sie Javascript verwenden.

ul { 
 
    background-image: url('../img/bg.jpg'); 
 
    background-repeat: no-repeat; 
 
    background-origin: border-box; 
 
    background-position:right top; 
 
    border-top: 1px solid #444; 
 
    border-bottom: 1px solid #444; 
 
    border-right: 1px solid #444; 
 
    display: inline-block; 
 
    height: 700px; 
 
    position: relative; 
 
    width: 800px; 
 
    list-style:none; 
 
    padding-left:0; 
 
} 
 
li { 
 
    font: bold 16px/100px sans-serif; 
 
    height: 100px; 
 
} 
 
a { 
 
    border-right: 1px solid #444; 
 
    border-top: 1px solid #444; 
 
    border-left: 1px solid #444; 
 
    color: black; 
 
    display: block; 
 
    text-align: center; 
 
    text-decoration: none; 
 
    width: 99px;  
 
} 
 
a:hover { 
 
    background: gray; 
 
    color: #fff; 
 
} 
 
div.relative { 
 
    position: relative; 
 
    width: 700; 
 
    height: 900; 
 
} 
 

 
div.absolute { 
 
    position: absolute; 
 
    top: 700px; 
 
    width: 800px; 
 
    height: 100px; 
 
    border: 1px solid #444; 
 
} 
 
h3{ 
 
    margin-left:200px; 
 
    margin-top:500px; 
 
} 
 
img { 
 
    height: 700; 
 
    position: absolute; 
 
    right: 0; 
 
    top: 0; 
 
    left: 101px; 
 
    background-color:red; 
 
    width: 600px; 
 
} 
 
.wrapper { 
 
    display: none; 
 
} 
 
a:hover + .wrapper { 
 
    display: block; 
 
}
<div class="relative"> 
 
    <ul> 
 
    <li> 
 
     <a href="#">Arms</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/309/?random" alt=""> 
 
     <div class="absolute">TEXT TEST 1</div> 
 
     </div> 
 
    </li> 
 
    <li> 
 
     <a href="#">Back</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/308/?random" alt=""> 
 
     <div class="absolute">TEXT TEST 2</div> 
 
     </div> 
 
    </li> 
 
    <li> 
 
     <a href="#">Head</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/306/?random" alt=""> 
 

 
     </div></li> 
 
    <li> 
 
     <a href="#">Feet</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/305/?random" alt=""> 
 

 
     </div></li> 
 
    <li> 
 
     <a href="#">Legs</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/304/?random" alt=""> 
 

 
     </div></li> 
 
    <li> 
 
     <a href="#">Chair</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/301/?random" alt=""> 
 
     </div> 
 
     </li> 
 
    <li> 
 
     <a href="#">Desk</a> 
 
     <div class="wrapper"> 
 
     <img src="https://unsplash.it/1200/302/?random" alt=""> 
 

 
     </div></li> 
 
    </ul> 
 
</div>

1

können Sie fügen Sie einfach einen Stil, den Text div ein- und ausblenden, wie Sie bereits für das img tun:

a + img + div 
{ 
    display: none; 
} 
a:hover + img + div 
{ 
    display: block; 
} 
+0

danke das funktioniert super! –

0

ich es mit JavaScript und jQuery gelöst. Aber ich bin mir nicht sicher, ob es dein Problem löst.

Fügen Sie einfach einen eventlistener zu allen li hinzu, der den Text des entsprechenden div ändert.

$("li").mouseover(function(){ 
     $("#TEXT1").text($(this).children("a").text()); 
    }) 

Sie können einen Tag alle geben Ihre li wenn Sie einige, die Sie wollen auf diese Weise nicht handeln.