Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background image when hovering on a list item

As you can see in the screenshot, I've got an unordered list. Now the div of this list has a background image. What I want to do is to change background's image whenever I hover the mouse on a list item. Note that every item should change background to a different image. How do I do this? I've only found answers how to change to a single, not multiple images.

Here's the screenshot.

enter image description here

I've already hovered on the first item of the list.

div's CSS:

.body {
  display: block;
  float: left;
  background-image: url("bgdef.jpg");
  background-repeat: no-repeat;
  position: static;
  width: 100%;
  height: auto;
  margin: 0;
}

.menu {
  width: 250px;
  padding: 0;
  margin: 100px 0px 33% 75%;
  list-style-type: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
}

.menu a:link,
a:visited,
a:hover,
a:active {
  text-decoration: none;
  bottom: auto;
  padding-bottom: auto;
  text-shadow: none;
}

.menu li {
  background-color: white;
  margin: 10px;
  padding: 3px 0 3px 10%;
  border-radius: 10PX 0 0 10px;
  font-size: 20px;
}

.menu li:hover {
  background-color: green;
  margin-right: 18px;
  margin-left: 1px;
}
like image 734
Karolis.L Avatar asked Sep 06 '15 06:09

Karolis.L


People also ask

How can I change background of image on hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How do I change the scroll image?

You can use the onScroll event to listen for scrolling, check at what position the scrollbar is and make the image change or whatever your heart desires. You can read more about onScroll event here. A basic code will be something like this: var onScrollHandler = function() { var newImageUrl = yourImageElement.

How do you change the scrolling background in CSS?

Set the background-size to "cover" to scale the images as large as possible to cover all the background area. Add links of the images with the background-image property. Style the content giving it a border and setting the width and height of it. Set the position to "fixed" so as it will be fixed while scrolling.

How can I increase hover background-size?

Padding creates space inside of the element. The more padding you give, the bigger the background becomes on hover.


2 Answers

You can do this by CSS only. It's a trick and in the most of the cases you will need to use JS, but its working and working good! (See it in Full Page)

.wrapper {
  width:900px;
  height:600px;
  position:relative;
}

.item {
  position:relative;
  z-index:1;
}

.bg {
  position:absolute;
  top:0;
  left:0;
      width: 100%;
    height: 100%;
}

.bg img {
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  opacity:0;
  transition:all .3s ease;
}

.bg img:nth-child(1),
.item:nth-child(1):hover ~ .bg img:nth-child(1),
.item:nth-child(2):hover ~ .bg img:nth-child(2),
.item:nth-child(3):hover ~ .bg img:nth-child(3) {
  opacity:1;
}
<div class="wrapper">
    <div class="item">Item 1</div>
    <div class="item">Item 2</div>
    <div class="item">Item 3</div>
    <div class="bg">
      <img src="http://i.stack.imgur.com/tq1uR.jpg" />
      <img src="http://i.stack.imgur.com/ZAy9V.jpg" />
      <img src="http://i.stack.imgur.com/xfvXS.jpg" />
    </div>
  </div>
like image 137
Mosh Feu Avatar answered Oct 04 '22 07:10

Mosh Feu


Since there is still no CSS parent selector you can use jQuery

#container {
    width: 800px;
    height: 600px;
    background: url(http://filepic.ru/file/1441522670.jpg);
}
#container li {
    display: block;
    margin: 5px;
    float: right;
    clear: both;
    background-color: #fff;
    width: 150px;
}
#container li:hover {
    background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
  <ul>
    <li onmouseover="$('#container').css('background','url(http://filepic.ru/file/1441522623.jpg)');">1</li>
    <li onmouseover="$('#container').css('background','url(http://filepic.ru/file/1441522645.jpg)');">2</li>
    <li onmouseover="$('#container').css('background','url(http://filepic.ru/file/1441522653.jpg)');">3</li>
    <li onmouseover="$('#container').css('background','url(http://filepic.ru/file/1441522662.png)');">4</li>
    <li onmouseover="$('#container').css('background','url(http://filepic.ru/file/1441522670.jpg)');">5</li>
  </ul>
</div>
like image 26
Cheslab Avatar answered Oct 04 '22 08:10

Cheslab