Table of Contents
HTML page
JavaScript部分
Home Web Front-end HTML Tutorial Create apple TV poster parallax effect using CSS3 and jQuery

Create apple TV poster parallax effect using CSS3 and jQuery

Jun 26, 2017 pm 03:17 PM
apple css css3 jquery create poster

Use CSS and jQuery to implement it, try to look the same as the original effect.


Final rendering

In this tutorial, I will use CSS, HTML and jQuery to create an approximate Apple TV parallax effect. If you Reading this, I assume you have a basic understanding of the above three techniques.

Without further ado, let’s start the first part.

HTML page

Our page structure is as follows:

<div class="poster">  <div class="shine"></div>
  <div class="layer-1"></div>
  <div class="layer-2"></div>
  <div class="layer-3"></div>
  <div class="layer-4"></div>
  <div class="layer-5"></div>
</div>
Copy after login

First, we need a div# with the style class .poster ##, this div contains 5 other style layers div. On top of these five layers div there is a shine div to add some sparkle.

CSS Part

First, add the following code to ensure that the height of the

body part of the web page is the entire page height:

body, html { height: 100%; min-height: 100%; }
Copy after login

and then

body Some background gradient colors:

body { background: linear-gradient(to bottom, #f6f7fc 0%,#d5e1e8 40%); }
Copy after login

In order to give

.poster a 3D rotation effect, the parent container needs to set perspective and transformation effects. As we can see, the parent container of div is body itself, so add the following CSS code:

body {background: linear-gradient(to bottom, #f6f7fc 0%,#d5e1e8 40%);transform-style: preserve-3d;transform: perspective(800px);
}
Copy after login

Now set the style and size of the card so that it fits on the page Center, add some rounded corners and shadow effects:

.poster {width: 320px;height: 500px;position: absolute;top: 50%; left: 50%;margin: -250px 0 0 -160px;border-radius: 5px;box-shadow: 0 45px 100px rgba(0, 0, 0, 0.4);overflow:hidden;
}
Copy after login

In order to center the poster, you need to set the value of

position to absolute, top:50% , 'left:50%', the upper margin value is a negative number that is half the height of div, and the left margin value is div A negative number that is half the width. What needs to be remembered is that the center of .poster is also the center of the entire page.


Shadow effect
We can use the following CSS selector to select all layers:

div[class *= &#39;layer-&#39;]
Copy after login

.poster has been designed, let’s see the effect.

So, CSS selects all

div that contain "layer-" in their class names.

Now, set the

position value of all layers to absolute, and the background-repeat value to no-repeat , background-position is top left, and the size of the layer background is 100% width and automatic height.

div[class*="layer-"] {position: absolute;top: -10px; left: -10px;right: -10px; bottom: -10px;background-size: 100% auto;background-repeat: no-repeat;background-position: 0 0;transition:0.1s;
}
Copy after login

noticed that the values ​​​​of

top,left,right,bottom are all -10px, the purpose is to make The size of the layer is 20px larger than poster, so that when inspecting the effects of each layer, the edges of the layer will not be visible.

The following is to add a background to each layer:

.layer-1 {background-image: url(&#39;http://designmodo.com/demo/apple-tv-parallax/images/1.png&#39;);
}.layer-2 {background-image: url(&#39;http://designmodo.com/demo/apple-tv-parallax/images/2.png&#39;);
}.layer-3 {top: 0; bottom: 0;left: 0; right: 0;background-image: url(&#39;http://designmodo.com/demo/apple-tv-parallax/images/3.png&#39;);
}.layer-4 {background-image: url(&#39;http://designmodo.com/demo/apple-tv-parallax/images/4.png&#39;);
}.layer-5 {background-image: url(&#39;http://designmodo.com/demo/apple-tv-parallax/images/5.png&#39;);
}
Copy after login

In the

layer-3 part, the layer will not move, so the size does not need to be too large.


Complete static effect

JavaScript部分

在开始之前,请确保已经引入了jQuery库,否则会报错的。

视差效果的逻辑是这样的,每当鼠标移动的时候,根据光标的位置,.postertransforms:translateY,rotate,rotateY 属性将会改变。光标距离页面左上角越远,动画的效果越明显。

公式就类似于这样的:offsetX=0.5-光标距离页面顶端的位置/宽度。

为了每个元素的值都不一样,将给每一个光标公式返回的值乘以一个自定义的值,返回HTML的代码给每个会有动画的层元素添加 data-offset=数字 的属性。

<div data-offset="15" class="poster">
    <div class="shine"></div>
    <div data-offset="-2" class="layer-1"></div>
    <div class="layer-2"></div>
    <div data-offset="1" class="layer-3"></div>
    <div data-offset="3" class="layer-4"></div>
    <div data-offset="10" class="layer-5"></div>
</div>
Copy after login

每一个 .layers 的规则都相同,但是我们给他们应用到 translateYtranslateX 属性上。

data-offset 属性的值越大,动画的效果越明显,可以改变这些值体验下。

为了代码可读性,我们在JavaScript里给 .poster 赋值给 $poster 变量,.shine$shine 变量,$layer 变量代表所有层,w,h代表页面的宽度和高度。

var $poster = $(&#39;.poster&#39;),$shine = $(&#39;.shine&#39;),$layer = $(&#39;div[class*="layer-"]’);
Copy after login

现在,需要考虑下当光标移动的时候获取到光标位置的问题。我们可以用 $(window)mousemove 事件来实现,这个事件会返回一个JavaScript对象,含有我们需要的位置信息和其他一些我们暂时还用不到的变量。

$(window).on(&#39;mousemove&#39;, function(e) {var w=e.currentTarget.innerWidth,h=e.currentTarget.innerHeight;var offsetX = 0.5 - e.pageX / w, /* where e.pageX is our cursor X coordinate */
    offsetY = 0.5 - e.pageY / h,
    offsetPoster = $poster.data(&#39;offset&#39;), /* custom value for animation depth */
    transformPoster = &#39;translateY(&#39; + -offsetX * offsetPoster + &#39;px) rotateX(&#39; + (-offsetY * offsetPoster) + &#39;deg) rotateY(&#39; + (offsetX * (offsetPoster * 2)) + &#39;deg)&#39;;/* apply transform to $poster */
    $poster.css(&#39;transform&#39;, transformPoster);/* parallax foreach layer *//* loop thought each layer *//* get custom parallax value *//* apply transform */
    $layer.each(function() {var $this = $(this);var offsetLayer = $this.data(&#39;offset&#39;) || 0; /* get custom parallax value, if element docent have data-offset, then its 0 */var transformLayer = &#39;translateX(&#39; + offsetX * offsetLayer + &#39;px) translateY(&#39; + offsetY * offsetLayer + &#39;px)&#39;;

        $this.css(&#39;transform&#39;, transformLayer);
    });
});
Copy after login

下一步,就是用上面解释的公式来计算offsetYoffsetX的值,然后就是把视差效果应用到.posert和每一个海报层。

非常酷啊,现在我们就有了一个有视差效果的小部件了。


基本完成

但是还没完,海报上的光泽部分还没设置

现在回到CSS部分,给.shine div 绝对定位,添加一个渐变颜色效果,设置z-index属性值为100,让它在所有层的上面。

.shine {position: absolute;top: 0; left: 0; right: 0; bottom: 0;background: linear-gradient(90deg, rgba(255,255,255,.5) 0%,rgba(255,255,255,0) 60%);z-index: 100;
}
Copy after login

已经有了一个漂亮的闪光层在海报上,但是为了达到更逼真的效果,光照应该随着光标的移动而变化。


更逼真些

我们怎么做呢?可能你还记得无聊的初三数学课,当你想着你在学一些你从来都不会用到的公式的时候,我们现在就用到了。

所以,倾斜的角度应该等于光标与海报中心形成三角形的角度的相反值。(还记得吧,海报的中心就是整个页面的中心啊,也就是页面宽度和高度的二分之一)


角度示意图

首先,找到光标与页面中心形成的三角形的直角边,光标与中心连线后作出一个直角三角形。

然后用 Math.atan2() 函数得到中心点的角度值。注意这个函数的返回值使用弧度值来表示的,所以我们得在CSS中转换成角的度数,用以下公式:

弧度值*180/pi = 角度值

var $poster = $(&#39;.poster&#39;);var $shine = $(&#39;.shine&#39;);var $layer = $(&#39;div[class *= "layer-"]&#39;);

    $poster.data("offset",15);

    $(window).on(&#39;mousemove&#39;, function(e) {var w=e.currentTarget.innerWidth,h=e.currentTarget.innerHeight;var offsetX = 0.5 - e.pageX / w, /* where e.pageX is our cursor X coordinate */
        offsetY = 0.5 - e.pageY / h,
        offsetPoster = $poster.data(&#39;offset&#39;), /* custom value for animation depth */
        transformPoster = &#39;translateY(&#39; + -offsetX * offsetPoster + &#39;px) rotateX(&#39; + (-offsetY * offsetPoster) + &#39;deg) rotateY(&#39; + (offsetX * (offsetPoster * 2)) + &#39;deg)&#39;;

        dy = e.pageY - h / 2,
        dx = e.pageX - w / 2,
        theta = Math.atan2(dy,dx), /* get angle in radians */
        angle = theta * 180 / Math.PI; /* convert rad in degrees *//* apply transform to $poster */
        $poster.css(&#39;transform&#39;, transformPoster);/* parallax foreach layer *//* loop thought each layer *//* get custom parallax value *//* apply transform */
        $layer.each(function() {var $this = $(this);var offsetLayer = $this.data(&#39;offset&#39;) || 0; /* get custom parallax value, if element docent have data-offset, then its 0 */var transformLayer = &#39;translateX(&#39; + offsetX * offsetLayer + &#39;px) translateY(&#39; + offsetY * offsetLayer + &#39;px)&#39;;

            $this.css(&#39;transform&#39;, transformLayer);
        });
    });
Copy after login

你会发现角度值的范围是从-180到180度,以下代码修复这个问题让角度值从0-360度:

if (angle < 0) {angle = angle + 360;
}
Copy after login

现在角度有了,就可以随着光标的移动来动态改变渐变颜色的角度值:

<code class="lsl">$shine.css(&#39;background&#39;, &#39;linear-gradient(&#39; + (angle - <span class="hljs-number">90) + &#39;deg, rgba(<span class="hljs-number">255,<span class="hljs-number">255,<span class="hljs-number">255,&#39; + e.pageY / h + &#39;) <span class="hljs-number">0%,rgba(<span class="hljs-number">255,<span class="hljs-number">255,<span class="hljs-number">255,<span class="hljs-number">0) <span class="hljs-number">80%)&#39;);<br/><br/></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入学习交流群
343599877,我们一起学前端!

注意 :减去90度的原因是 linear-gradient 属性的需要,如果你使用 -webkit-linear-gradient,-moz-linear-gradient属性就没有必要。

转译

The above is the detailed content of Create apple TV poster parallax effect using CSS3 and jQuery. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove the default style in Bootstrap list? How to remove the default style in Bootstrap list? Apr 07, 2025 am 10:18 AM

The default style of the Bootstrap list can be removed with CSS override. Use more specific CSS rules and selectors, follow the "proximity principle" and "weight principle", overriding the Bootstrap default style. To avoid style conflicts, more targeted selectors can be used. If the override is unsuccessful, adjust the weight of the custom CSS. At the same time, pay attention to performance optimization, avoid overuse of !important, and write concise and efficient CSS code.

How to do PS gradient color picker How to do PS gradient color picker Apr 06, 2025 pm 10:09 PM

Gradient color pickers give designers the flexibility to extract and create gradients from images. It simplifies gradient creation, ensures accuracy, inspires, improves efficiency and provides cross-platform support, and covers a wide range of applications including websites, graphic design, UI/UX design and digital art.

Does the image centering support image zooming? Does the image centering support image zooming? Apr 07, 2025 am 07:42 AM

How to achieve image centering and scaling in Bootstrap: Use d-flex justify-content-center to center images horizontally. Use align-items-center and fixed parent element height vertically center the image. Use the width and height attributes to control the image size, or use max-width and max-height to limit the maximum size. Use the img-fluid class or responsive design mechanism, such as media queries, to achieve responsive scaling. Optimize image size, control scaling using the object-fit attribute, and follow best practices to ensure performance and maintainability.

How to build a bootstrap framework How to build a bootstrap framework Apr 07, 2025 pm 12:57 PM

To create a Bootstrap framework, follow these steps: Install Bootstrap via CDN or install a local copy. Create an HTML document and link Bootstrap CSS to the &lt;head&gt; section. Add Bootstrap JavaScript file to the &lt;body&gt; section. Use the Bootstrap component and customize the stylesheet to suit your needs.

How to view Bootstrap's grid system How to view Bootstrap's grid system Apr 07, 2025 am 09:48 AM

Bootstrap's mesh system is a rule for quickly building responsive layouts, consisting of three main classes: container (container), row (row), and col (column). By default, 12-column grids are provided, and the width of each column can be adjusted through auxiliary classes such as col-md-, thereby achieving layout optimization for different screen sizes. By using offset classes and nested meshes, layout flexibility can be extended. When using a grid system, make sure that each element has the correct nesting structure and consider performance optimization to improve page loading speed. Only by in-depth understanding and practice can we master the Bootstrap grid system proficiently.

How to view the CSS style of Bootstrap How to view the CSS style of Bootstrap Apr 07, 2025 am 10:24 AM

How to view Bootstrap CSS: Using Browser Developer Tools (F12). Find the "Elements" or "Inspector" tab and find the Bootstrap component. View the CSS styles that the component applies in the Styles panel. Developer tools can be used to filter styles or debug code to gain insight into how it works. Proficient in developer tools and avoid detours.

How to set the bootstrap navigation bar How to set the bootstrap navigation bar Apr 07, 2025 pm 01:51 PM

Bootstrap provides a simple guide to setting up navigation bars: Introducing the Bootstrap library to create navigation bar containers Add brand identity Create navigation links Add other elements (optional) Adjust styles (optional)

How to layout bootstrap How to layout bootstrap Apr 07, 2025 pm 02:24 PM

To use Bootstrap to layout a website, you need to use a grid system to divide the page into containers, rows, and columns. First add the container, then add the rows in it, add the columns within the row, and finally add the content in the column. Bootstrap's responsive layout function automatically adjusts the layout according to breakpoints (xs, sm, md, lg, xl). Different layouts under different screen sizes can be achieved by using responsive classes.

See all articles