{"id":70,"date":"2014-05-29T08:37:04","date_gmt":"2014-05-29T08:37:04","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=70"},"modified":"2014-05-29T15:04:49","modified_gmt":"2014-05-29T15:04:49","slug":"paralax-effect-on-page","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=70","title":{"rendered":"simple paralax effect on page"},"content":{"rendered":"<blockquote><p><em>Goal: simulate 3d effect on the page.<\/em><\/p><\/blockquote>\n<p>To to this we need minimum two layers. The foreground layer moving faster than the background. That&#8217;s all.<br \/>\nThe foreground layer is the page itself. We need to create a slower background layer.<\/p>\n<p>To do this we need a container DIV on the page. The size of this contanier is fit to the page size. Plus we need a second one DIV with the background image to create the effect.<\/p>\n<p><code data-enlighter-language=\"html\" class=\"EnlighterJSRAW\">&lt;div id=&quot;backcont&quot;&gt;&lt;div id=&quot;backfly&quot;&gt;&lt;\/div&gt;&lt;\/div&gt;<\/code><\/p>\n<p>and the style is<\/p>\n<pre data-enlighter-language=\"css\" class=\"EnlighterJSRAW\">\r\n#backcont{\r\n  position: fixed;\r\n  top: 0;\r\n  left: 0;\r\n  width: 100%;\r\n  height: 100%;\r\n  margin: 0;\t\r\n  overflow: hidden;\r\n  z-index: 0;\r\n}\r\n#backfly{\r\n  position:absolute;\r\n  top:0;\r\n  left:0;\r\n  width:130%;\r\n  height:130%;\r\n  z-index: 0;\r\n  background: url(&quot;\/images\/something.jpg&quot;);\t\r\n}\r\n<\/pre>\n<p>This is the base. Let&#8217;s coding! First of all we need to know that the size <code data-enlighter-language=\"css\" class=\"EnlighterJSRAW\">height: 100%; width: 100%;<\/code> not enough because the page real size is not this. This is the screen size. We need to correct it later. Watch the code:<\/p>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\r\n$(document).ready(function(){\r\n\r\n  var $backcont = $('#backcont');\r\n  var $backfly = $('#backfly');\t\r\n\r\n  function sizeCorrection(){\r\n    var hmax = $(document).height();\r\n    var wmax = $(document).width();\r\n    $backcont.height(hmax).width(wmax);\r\n    $backfly.height(hmax*1.3).width(wmax*1.3); \/* will be bigger than the page! *\/\r\n  };\r\n  sizeCorrection();\r\n\r\n  $(window).on('scroll resize', function(){\r\n    sizeCorrection();\r\n    var t = $(window).scrollTop(),\r\n        l = $(window).scrollLeft();\r\n    $backfly.css({left:-l\/10, top: -t\/10});\r\n  });\r\n\r\n});\r\n<\/pre>\n<p>In fact we have 3 layers.<br \/>\n<em><br \/>\n1. the page itself<br \/>\n2. the slower moving (sluggish) background<br \/>\n3. fixed elements like the browser window<br \/>\n   or other fixed elements if we have on the page (here is the sidebar for example)<br \/>\n<\/em><\/p>\n<p>The result is a 3d like view.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Goal: simulate 3d effect on the page. To to this we need minimum two layers. The foreground layer moving faster than the background. That&#8217;s all. The foreground layer is the page itself. We need to create a slower background layer. To do this we need a container DIV on the page. The size of this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-web-trick"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":21,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":112,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions\/112"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}