jQuery Image Hover Captions by http://alijafarian.com/jquery-image-hover-captions/

Today we’re going to build some simple jQuery image hover captions. These types of captions work very well in a variety of grid formats – portfolio work, feature boxes, blog displays, etc. From a UX perspective they attract user attention [with images] and then display additional content on hover. Let’s get started!

The HTML

We’ll create a standard container and then child .block divs inside. The .block divs will contain the images and child .caption divs that contain the caption content.
  1. <div class="grid-block-container">
  2.  
  3. <div class="grid-block standard">
  4. <div class="caption">
  5. <h3>Caption Title</h3>
  6. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  7. <p><a href="#" class="learn-more">Learn more</a></p>
  8. </div>
  9. <img src="images/image-001.jpg" />
  10. <h4>Normal</h4>
  11. </div>
  12. <!--/.grid-block-->
  13.  
  14. <div class="grid-block fade">
  15. <div class="caption">
  16. <h3>Caption Title</h3>
  17. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  18. <p><a href="#" class="learn-more">Learn more</a></p>
  19. </div>
  20. <img src="images/image-002.jpg" />
  21. <h4>Fade</h4>
  22. </div>
  23. <!--/.grid-block-->
  24.  
  25. <div class="grid-block slide">
  26. <div class="caption">
  27. <h3>Caption Title</h3>
  28. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
  29. <p><a href="#" class="learn-more">Learn more</a></p>
  30. </div>
  31. <img src="images/image-003.jpg" />
  32. <h4>Slide</h4>
  33. </div>
  34. <!--/.grid-block-->
  35.  
  36. </div>
  37. <!--/.grid-container-->

The CSS

We need float the container left with a negative left margin of 30px. This allow us to float the .block divs left with a left margin of 30px to keep the grid format. We’ll also assign a height to the .block divs which is the height of the image. Note: this is ONLY needed if you have HTML below the image caption such as a title or sub paragraph.
  1. .grid-block-container {
  2. float: left;
  3. width: 990px;
  4. margin: 20px 0 0 -30px;
  5. }
  6. .grid-block {
  7. position: relative;
  8. float: left;
  9. width: 300px;
  10. height: 200px;
  11. margin: 0 0 30px 30px;
  12. }
  13. .grid-block h4 {
  14. font-size: .9em;
  15. color: #333;
  16. background: #f5f5f5;
  17. margin: 0;
  18. padding: 10px;
  19. border: 1px solid #ddd;
  20. }
  21.  
  22. .caption {
  23. display: none;
  24. position: absolute;
  25. top: 0;
  26. left: 0;
  27. background: url(images/trans-black-50.png);
  28. width: 100%;
  29. height: 100%;
  30. }
  31. .caption h3, .caption p {
  32. color: #fff;
  33. margin: 20px;
  34. }
  35. .caption h3 {
  36. margin: 20px 20px 10px;
  37. }
  38. .caption p {
  39. font-size: .75em;
  40. line-height: 1.5em;
  41. margin: 0 20px 15px;
  42. }
  43. .caption a.learn-more {
  44. padding: 5px 10px;
  45. background: #08c;
  46. color: #fff;
  47. border-radius: 2px;
  48. -moz-border-radius: 2px;
  49. font-weight: bold;
  50. text-decoration: none;
  51. }
  52. .caption a.learn-more:hover {
  53. background: #fff;
  54. color: #08c;
  55. }

The jQuery

And finally, the finishing touches with jQuery. We’ll be using the .hover event for the hover interaction (no surprise there!), and the .find traversal to make the function global. This allows us to write one function that applies to an element with a certain class, instead of writing several functions specifically targeted at id’s. You’ll also see that I included 3 different versions – a standard hide/show, a fade in/out, and a slide down/up.
  1. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  2. <script type="text/javascript">
  3. $(document).ready(function() {
  4. $('.standard').hover(
  5. function(){
  6. $(this).find('.caption').show();
  7. },
  8. function(){
  9. $(this).find('.caption').hide();
  10. }
  11. );
  12. $('.fade').hover(
  13. function(){
  14. $(this).find('.caption').fadeIn(250);
  15. },
  16. function(){
  17. $(this).find('.caption').fadeOut(250);
  18. }
  19. );
  20. $('.slide').hover(
  21. function(){
  22. $(this).find('.caption').slideDown(250);
  23. },
  24. function(){
  25. $(this).find('.caption').slideUp(250);
  26. }
  27. );
  28. });
  29. </script>

Summary

And there you have it! A simple [and elegant] way to add hover captions to your images. I hope this taught you something new or ends up helping you with a project. Feel free to leave questions/comments below.

Nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com

MVC định dạng tiền tệ vnđ - Format currency vnd in MVC