Advertisement
Guest User

Parallax WordPress thumbnail

a guest
Jul 25th, 2016
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. Functions.php:
  2.  
  3. function add_parallax($post) {
  4. $parallax = "<div id='parallax-banner'>";
  5. $parallax .= get_the_post_thumbnail( $post, 'full' );
  6. // $parallax .= "<span class='title'>".get_the_title($post)."</span>";
  7. $parallax .= "</div>";
  8.  
  9. echo $parallax;
  10. }
  11.  
  12. CMS Control PHP code:
  13.  
  14. <?php
  15. if( is_singular() ) {
  16. add_parallax($parallax[0]->ID);
  17. }
  18. ?>
  19.  
  20. <script>
  21. jQuery(window).load(function(){
  22. var $banner = jQuery("#parallax-banner"), $image = $banner.find("img"), height = 0, max = 0;
  23.  
  24. //let's find the best height for it
  25. height = $banner.find("img").height() / 3;
  26. $banner.height(height);
  27.  
  28. //this is the maxiumum amount of scroll for still visible banner, cached to improve performance
  29. max = ($banner.offset()).top + height;
  30.  
  31. //let's position our title in the middle of the Area
  32. $banner.find(".title").css({
  33. "top": ((height - $banner.find(".title").height()) / 2) + "px"
  34. });
  35.  
  36. jQuery(window).scroll(function() {
  37. parallax_image(max, $image);
  38. });
  39. });
  40.  
  41. function parallax_image(max, $image) {
  42. var imgTop = 0, scrollPos = jQuery(this).scrollTop();
  43.  
  44. //rule of thirds to find the new top position for image
  45. imgTop = (scrollPos / max) * (2/3) * $image.height();
  46.  
  47. $image.css({
  48. "top": -(imgTop)+"px"
  49. });
  50. }
  51. </script>
  52.  
  53. CMS Control CSS Code:
  54.  
  55. #parallax-banner {
  56. position: relative;
  57. width: 100%;
  58. margin-left: 0;
  59. overflow: hidden;
  60. }
  61. #parallax-banner img {
  62. position: absolute;
  63. }
  64.  
  65. #parallax-banner .title {
  66. position: absolute;
  67. padding: 5px 0;
  68. top: 0;
  69. left: 0;
  70. width: 100%;
  71. text-align: center;
  72. text-transform: uppercase;
  73. font-weight: bold;
  74. background-color: transparent;
  75. background-color: rgba(255, 255, 255, 0.8);
  76.  
  77. -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ccffffff,endColorstr=#ccffffff)";
  78.  
  79. -moz-box-shadow: 0 0 4px #000;
  80. -webkit-box-shadow: 0 0 4px #000;
  81. box-shadow: 0 0 4px #000;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement