只是另一个jQuery/JavaScript视差插件,用于使用CSS3 3D转换(translate3d)为图像、背景图像、Youtube/Vimeo/HTML5视频和任何HTML元素添加平滑的视差滚动效果。
回退到背景位置
当不支持CSS转换时。
1.在HTML页面中加载jarallax插件。jQuery库是可选的。
- <!-- jQuery (OPTIONAL) -->
- <script src="/path/to/cdn/jquery.min.js"></script>
- <!-- Core -->
- <script src="/path/to/dist/jarallax.js"></script>
- <!-- Video Support -->
- <script src="/path/to/dist/jarallax-video.js"></script>
- <!-- Custom Element Support -->
- <script src="/path/to/dist/jarallax-element.js"></script>
2.将视差图像添加到容器中,如下所示。
- <!-- img tag -->
- <div class="jarallax">
- <img class="jarallax-img" src="bg.jpg" alt="">
- </div>
- <!-- picture tag -->
- <div class="jarallax">
- <picture class="jarallax-img">
- <source media="(max-width: 650px)" srcset="bg-alt.jpg">
- <img src="bg.jpg" alt="">
- </picture>
- </div>
- <!-- background image -->
- <div class="jarallax" style="background-image: url('bg.jpg');">
- </div>
3.背景图像所需的CSS样式。
- .jarallax {
- position: relative;
- z-index: 0;
- }
- .jarallax > .jarallax-img {
- position: absolute;
- object-fit: cover;
- /* support for plugin https://github.com/bfred-it/object-fit-images */
- font-family: 'object-fit: cover;';
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- z-index: -1;
- }
4.初始化视差插件的最致命方法是将datajarallax属性添加到视差容器中。您可以通过数据OPTION属性传递配置选项,如下所示:
- <div class="jarallax"
- data-jarallax
- data-OPTIONNAME="VALUE">
- <img class="jarallax-img" src="bg.jpg" alt="">
- </div>
5.您也可以通过JavaScript初始化视差插件:
- // Vanilla JavaScript
- jarallax(document.querySelectorAll('.jarallax'), {
- // options here
- });
- // Or jQuery
- $('.jarallax').jarallax({
- // options here
- });
6.将视差滚动效果应用于视频:
- <div class="jarallax" data-jarallax-video="https://www.youtube.com/watch?v=VIDEO ID HERE">
- Youtube Video
- </div>
- <div class="jarallax" data-jarallax-video="https://vimeo.com/VIDEO ID HERE">
- Vimeo
- </div>
- <div class="jarallax" data-jarallax-video="mp4:./1.mp4,webm:./1,ogv:./1.ogv">
- HTML5 Video
- </div>
- // Or Via JavaScript
- jarallax(document.querySelectorAll('.jarallax'), {
- videoSrc: 'VIDEO SOURCE HERE'
- });
- // Or Via jQuery
- $('.jarallax').jarallax({
- videoSrc: 'VIDEO SOURCE HERE'
- });
7.将视差滚动效果应用于HTML元素:
- <!-- Element will be parallaxed on -150 pixels from the screen center by Y axis -->
- <div data-jarallax-element="-150">
- ...
- </div>
- <!-- Element will be parallaxed on 300 pixels from the screen center by Y axis and on -150 pixels from the screen center by X axis -->
- <div data-jarallax-element="300 -150">
- ...
- </div>
8.所有默认配置选项。
- // scroll, scale, opacity, scale-opacity, scroll-opacity
- type: 'scroll',
- // animation speed
- speed: 0.5,
- // image parallax scroll effect
- imgSrc: null,
- imgElement: '.jarallax-img',
- imgSize: 'cover',
- imgPosition: '50% 50%',
- imgRepeat: 'no-repeat',
- // keep <img> tag in it's default place
- keepImg: false,
- // use custom DOM / jQuery element to check if parallax block in viewport.
- elementInViewport: null,
- // z-index property
- zIndex: -100,
- // disable parallax scroll effect on certain devices
- // e.g. disableParallax: /iPad|iPhone|iPod|Android/
- disableParallax: false,
- // disable video parallax scroll effect on certain devices
- // e.g. disableParallax: /iPad|iPhone|iPod|Android/
- disableVideo: false,
- // use ResizeObserver API to recalculate position and size of parallax image
- automaticResize: true,
- // video options
- videoSrc: null,
- videoStartTime: 0,
- videoEndTime: 0,
- videoVolume: 0,
- videoLoop: true,
- videoPlayOnlyVisible: true,
- videoLazyLoading: true
9.回调函数。
- jarallax(document.querySelectorAll('.jarallax'), {
- onScroll: function(calculations) {},
- onInit: function() {},
- onDestroy: function() {},
- onCoverImage: function() {},
- // video extension
- onVideoInsert: function() {//this.$video},
- onVideoWorkerInit: function(videoWorkerObject) {},
- });
10.API方法。
- // Vanilla JS
- // jarallax(document.querySelectorAll('.jarallax'), method);
- // jQuery
- // $('.jarallax').jarallax(method);
- // destroy
- $('.jarallax').jarallax('destroy');
- // check if is visible
- $('.jarallax').jarallax('isVisible');
- // fit image and clip parallax container
- $('.jarallax').jarallax('onResize');
- // calculate parallax image position
- $('.jarallax').jarallax('scroll');
v2.0.4版本(2022-07-17)
v2.0.2版本(2022-02-17)
v2.0.1版本(2022-02-11)
v1.12.8 (2021-10-21)
v1.12.7 (2021-05-12)
v1.12.6 (2021-05-12)
v1.12.5 (2020-11-29)
v1.12.4 (2020-09-23)
v1.12.3 (2020-09-22)
v1.12.2 (2020-08-10)
v1.12.1 (2020-04-29)
v1.12.0 (2019-10-23)
v1.11.1 (2019-10-01)