0

I have a stack of li's. I'm trying to get the background color of a div to change upon mouse on and change back upon mouse off. I was able to figure out how to do it, but it's buggy. The gaps between the li's cause an issue where the jquery isn't triggered correctly when I hover from one li to another.

Here's my code: http://jsfiddle.net/blutuu/k93o28rf/8/

It's quite hacky, so I'm hoping for a better implementation. Thanks for your help.

blutuu
  • 590
  • 1
  • 5
  • 20
  • 1
    I don't see anything wrong. What browser are you using? –  Nov 02 '15 at 18:31
  • me either can't see anything strange; except for the hugly visual effect caused by { border-bottom: solid #0088BC 1px } that could i guess be removed cause it just looks as nothing.... – holden Nov 02 '15 at 18:57
  • I think what OP meant that the border between the `li`s `border-bottom: solid #0088BC 1px;` and the border at the right of the `li`s on hover `border-right: 25px solid #8CC63E;` breaks the hover effect. When you hover these areas `$('li').mouseout` is triggered. Using Chrome 46. – akinuri Nov 02 '15 at 19:06
  • Yeah, @akinuri is right. Same browse too. – blutuu Nov 02 '15 at 19:24
  • Please see my answer below. try using linear-gradient rather than border-right. – Cruiser Nov 02 '15 at 19:33
  • Alright, I've figured out what the problem is. You should use `mouseover` instead of `mouseenter` or `hover`. [jsFiddle](http://jsfiddle.net/akinuri/k93o28rf/11/). Check Navin's [answer](http://stackoverflow.com/a/17589540/2202732) for more information. – akinuri Nov 02 '15 at 21:23

4 Answers4

2

I'm stacking on top of both answers above. I did a little rearranging of the code and I think I finally got what you are looking for. I tucked the <li> tag inside of the <a> tags, at which point the entire element even when a border is added became clicable.

$(document).ready(function() {
  $('li').mouseenter(function() {
    var color = $(this).data('color');
    $('#mbg').css('background-color', color);
  });
  $('li').mouseout(function() {
    $('#mbg').css('background-color', 'blue');
  });
});
.resources {
  width: 17%;
  height: 100vh;
  overflow: hidden;
  position: absolute;
  z-index: 1;
  border-right: solid 1px #C5C5C5;
  box-shadow: 2px 0px 2px -1px #DCDCDC;
}
.resources ul {
  text-align: right;
  padding: 0;
  margin: auto 0;
}
.resources ul > li a {
  list-style-type: none;
  height: 65px;
  background: #00ADEF;
  border-bottom: solid #0088BC 1px;
  vertical-align: middle;
  overflow: hidden;
  padding: 0;
  box-shadow: 0px -1px 5px -2px #222 inset;
  box-sizing: border-box;
  transition: .5s;
}
.resources ul li a:hover {
  border-right: 25px solid #8CC63E;
  vertical-align: middle;
  overflow: hidden;
  /*transition: .5s;*/
}
.resources li a {
  line-height: 1em;
  text-decoration: none;
  color: white;
  font-size: 20px;
  font-weight: bold;
  display: block;
  padding: 1.13em;
}
#mbg {
  position: absolute;
  background-color: blue;
  width: 100%;
  height: 100vh;
  margin-left: 17%;
}
#layoutsTable {
  border: solid 1px #f08721;
  height: 100vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="mbody">
  <div class="resources">
    <ul>
      <a href="#">
        <li data-color="#380606">Policies</a>
      </li>
      <a href="#">
        <li data-color="#191919">LMS</a>
      </li>

      <a href="#">
        <li data-color="#cbcbcb">Tips & Tricks</a>
      </li>
      <li data-color="#f08721"><a href="#">Forms</a>
      </li>
    </ul>
  </div>
  <div id="mbg"></div>
</div>
Full Stack Alien
  • 11,244
  • 1
  • 24
  • 37
  • Thank you. Each answer was pretty similar, but this was the only one that worked like I wanted. – blutuu Nov 02 '15 at 20:00
1

Demo: http://jsfiddle.net/k93o28rf/6/

Use of bind to pass params directly to the changeColor function. Define a changeColor function so you don't have to define costly vars everytime. And simply call .css function on your div id to set background-color.

$(document).ready(function() {
    var content = $('#mbg');
    var changeColor = function(color) {
        content.css('background-color', color);
    }

    $('li').eq(0).hover(
        changeColor.bind(null, '#380606')
    );
    $('li').eq(1).hover(
        changeColor.bind(null, '#191919')
    );
    $('li').eq(2).hover(
        changeColor.bind(null, '#191919')
    );
    $('li').eq(3).hover(
        changeColor.bind(null, '#f08721')
    );
    $('li').mouseout(
        changeColor.bind(null, 'blue')
    );
});
Fanta1090
  • 41
  • 8
0

demo: http://jsfiddle.net/k93o28rf/3/

by using data tag attributes on each of the li elements, you can just have one mouseenter function and one mouseout function to handle the background color changes as shown below.

<div id="mbody">
    <div class="resources">
        <ul>
            <li data-color="#380606"><a href="https://sussex.sharepoint.com/SitePages/Policies.aspx">Policies</a></li>
            <li data-color="#191919"><a href="https://sussex.sharepoint.com/SitePages/LMS.aspx">LMS</a></li>
            <li data-color="#cbcbcb"><a href="https://sussex.sharepoint.com/SitePages/Tips%20and%20Tricks.aspx">Tips & Tricks</a></li>
            <li data-color="#f08721"><a href="https://sussex.sharepoint.com/SitePages/Forms.aspx">Forms</a></li>
        </ul>
    </div>
    <div id="mbg"></div>
</div>

$(document).ready(function() {
    $('li').mouseenter(function() {         
        var color = $(this).data('color');
        $('#mbg').css('background-color', color);
    });
    $('li').mouseout(function() {
        $('#mbg').css('background-color', 'blue');
    });
});
indubitablee
  • 8,136
  • 2
  • 25
  • 49
0

UPDATE: Try using linear-gradient instead of border-right on your a elements:

http://jsfiddle.net/em96edb0/

The problem is the border you've applied to the bottom. Add this to your a element in CSS:

box-sizing:conteny-box;

And that should fix it. Also, to be more efficient, use JQuery's .each function. Something like this:

$('li').each( ///your code );
Cruiser
  • 1,618
  • 2
  • 16
  • 20
  • This doesn't really solve the problem. Removing the `a` tags, however, does. No idea what's going on. [jsFiddle](http://jsfiddle.net/akinuri/k93o28rf/7/) – akinuri Nov 02 '15 at 19:16
  • Updated my answer, please see the fiddle. – Cruiser Nov 02 '15 at 19:33