16
2012Alternate BG Color Listing & Changing DIV Background Color on Hover using jQuery
Tags: jQueryFollowing is the simple solution to make a div’s background color change on mouse over and then change back to the original color on mouse out. I have made available all the php, css and js code for you to first create the listing and then changing the div background color on mouse hover.
Before going into the details, first check the demo below:
Here’s the js code. Just include the jQuery and the hover code in the head of your doc.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> $(document).ready(function() { $(".box").hover(function() { // Mouse Enter $(this).addClass("bg-hover"); }, function() { // Mouse Leave $(this).removeClass("bg-hover"); }); }); </script>
Here’s is the css:
.box{padding:10px;margin-bottom:5px; border: 1px #e6e6fa solid;} .bg1{ background-color: #f5f5f5;} .bg2{ background-color: #fffafa;} .bg-hover{ background-color: #fafae2;}
And here’s the markup with php code, assuming you have a result set of listing, this code will generate the listing with alternative bg color and mouse hover will highlight that div:
if(is_array($results)){ $i=0; foreach($results as $k){ ($i%2)? $bg_class ='bg1':$bg_class ='bg2'; echo '<div class="box '.$bg_class.'"> "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." </div>'; $i++; } }
Hope that helps.
Cheers!



Recent Comments
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)
(1 days ago)