jQuery和CSS3 高性能背景视差效应

  • 源码大小:3.36MB
  • 所需积分:1积分
  • 源码编号:19JP-3472
  • 浏览次数:718次
  • 最后更新:2023年06月17日
  • 所属栏目:动画
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

只是另一个jQuery/JavaScript视差插件,用于使用CSS3 3D转换(translate3d)为图像、背景图像、Youtube/Vimeo/HTML5视频和任何HTML元素添加平滑的视差滚动效果。

回退到背景位置当不支持CSS转换时。

基本用法:

1.在HTML页面中加载jarallax插件。jQuery库是可选的。

  1. <!-- jQuery (OPTIONAL) -->
  2. <script src="/path/to/cdn/jquery.min.js"></script>
  3. <!-- Core -->
  4. <script src="/path/to/dist/jarallax.js"></script>
  5. <!-- Video Support -->
  6. <script src="/path/to/dist/jarallax-video.js"></script>
  7. <!-- Custom Element Support -->
  8. <script src="/path/to/dist/jarallax-element.js"></script>

2.将视差图像添加到容器中,如下所示。

  1. <!-- img tag -->
  2. <div class="jarallax">
  3. <img class="jarallax-img" src="bg.jpg" alt="">
  4. </div>
  5.  
  6. <!-- picture tag -->
  7. <div class="jarallax">
  8. <picture class="jarallax-img">
  9. <source media="(max-width: 650px)" srcset="bg-alt.jpg">
  10. <img src="bg.jpg" alt="">
  11. </picture>
  12. </div>
  13.  
  14. <!-- background image -->
  15. <div class="jarallax" style="background-image: url('bg.jpg');">
  16. </div>

3.背景图像所需的CSS样式。

  1. .jarallax {
  2. position: relative;
  3. z-index: 0;
  4. }
  5. .jarallax > .jarallax-img {
  6. position: absolute;
  7. object-fit: cover;
  8. /* support for plugin https://github.com/bfred-it/object-fit-images */
  9. font-family: 'object-fit: cover;';
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. z-index: -1;
  15. }

4.初始化视差插件的最致命方法是将datajarallax属性添加到视差容器中。您可以通过数据OPTION属性传递配置选项,如下所示:

  1. <div class="jarallax"
  2. data-jarallax
  3. data-OPTIONNAME="VALUE">
  4. <img class="jarallax-img" src="bg.jpg" alt="">
  5. </div>

5.您也可以通过JavaScript初始化视差插件:

  1. // Vanilla JavaScript
  2. jarallax(document.querySelectorAll('.jarallax'), {
  3. // options here
  4. });
  5.  
  6. // Or jQuery
  7. $('.jarallax').jarallax({
  8. // options here
  9. });

6.将视差滚动效果应用于视频:

  1. <div class="jarallax" data-jarallax-video="https://www.youtube.com/watch?v=VIDEO ID HERE">
  2. Youtube Video
  3. </div>
  4.  
  5. <div class="jarallax" data-jarallax-video="https://vimeo.com/VIDEO ID HERE">
  6. Vimeo
  7. </div>
  8.  
  9. <div class="jarallax" data-jarallax-video="mp4:./1.mp4,webm:./1,ogv:./1.ogv">
  10. HTML5 Video
  11. </div>
  1. // Or Via JavaScript
  2. jarallax(document.querySelectorAll('.jarallax'), {
  3. videoSrc: 'VIDEO SOURCE HERE'
  4. });
  5.  
  6. // Or Via jQuery
  7. $('.jarallax').jarallax({
  8. videoSrc: 'VIDEO SOURCE HERE'
  9. });

7.将视差滚动效果应用于HTML元素:

  1. <!-- Element will be parallaxed on -150 pixels from the screen center by Y axis -->
  2. <div data-jarallax-element="-150">
  3. ...
  4. </div>
  5.  
  6. <!-- 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 -->
  7. <div data-jarallax-element="300 -150">
  8. ...
  9. </div>

8.所有默认配置选项。

  1. // scroll, scale, opacity, scale-opacity, scroll-opacity
  2. type: 'scroll',
  3.  
  4. // animation speed
  5. speed: 0.5,
  6.  
  7. // image parallax scroll effect
  8. imgSrc: null,
  9. imgElement: '.jarallax-img',
  10. imgSize: 'cover',
  11. imgPosition: '50% 50%',
  12. imgRepeat: 'no-repeat',
  13.  
  14. // keep <img> tag in it's default place
  15. keepImg: false,
  16.  
  17. // use custom DOM / jQuery element to check if parallax block in viewport.
  18. elementInViewport: null,
  19.  
  20. // z-index property
  21. zIndex: -100,
  22.  
  23. // disable parallax scroll effect on certain devices
  24. // e.g. disableParallax: /iPad|iPhone|iPod|Android/
  25. disableParallax: false,
  26.  
  27. // disable video parallax scroll effect on certain devices
  28. // e.g. disableParallax: /iPad|iPhone|iPod|Android/
  29. disableVideo: false,
  30.  
  31. // use ResizeObserver API to recalculate position and size of parallax image
  32. automaticResize: true,
  33.  
  34. // video options
  35. videoSrc: null,
  36. videoStartTime: 0,
  37. videoEndTime: 0,
  38. videoVolume: 0,
  39. videoLoop: true,
  40. videoPlayOnlyVisible: true,
  41. videoLazyLoading: true

9.回调函数。

  1. jarallax(document.querySelectorAll('.jarallax'), {
  2. onScroll: function(calculations) {},
  3. onInit: function() {},
  4. onDestroy: function() {},
  5. onCoverImage: function() {},
  6. // video extension
  7. onVideoInsert: function() {//this.$video},
  8. onVideoWorkerInit: function(videoWorkerObject) {},
  9. });

10.API方法。

  1. // Vanilla JS
  2. // jarallax(document.querySelectorAll('.jarallax'), method);
  3. // jQuery
  4. // $('.jarallax').jarallax(method);
  5.  
  6. // destroy
  7. $('.jarallax').jarallax('destroy');
  8.  
  9. // check if is visible
  10. $('.jarallax').jarallax('isVisible');
  11.  
  12. // fit image and clip parallax container
  13. $('.jarallax').jarallax('onResize');
  14.  
  15. // calculate parallax image position
  16. $('.jarallax').jarallax('scroll');

更新日志:

v2.0.4版本(2022-07-17)

  • 删除将更改用途

v2.0.2版本(2022-02-17)

  • 在视频扩展中全局添加了VideoWorker,因为一些项目使用了它
  • 已添加浏览器列表配置nk
  • 更新的esint和更漂亮的配置

v2.0.1版本(2022-02-11)

  • 删除了IE支持(仅支持现代浏览器)
  • 增加了ESM、UMD和CJS模块
  • 向视频扩展添加了新的事件回调:onVideoInsert、onVideoWorkerUnit
  • 添加了更漂亮的代码格式
  • 删除了clipContainer方法,改用现代剪辑路径样式
  • 删除了对带有供应商前缀的转换的检查

v1.12.8 (2021-10-21)

  • 在dist中包括TS打字员
  • 将移动设备的标准位置更改为“固定”(工作更顺畅)

v1.12.7 (2021-05-12)

  • 已删除安装后脚本

v1.12.6 (2021-05-12)

  • 为背景视频添加了辅助功能属性(tabindex、aria隐藏)

v1.12.5 (2020-11-29)

  • 向Youtube和Vimeo API调用添加了GDPR符合性参数(在video-workers库中)
  • 更新的依赖项

v1.12.4 (2020-09-23)

  • 视频元素的附加样式(防止点击视频块)
  • 修复了视频循环和图像重置问题

v1.12.3 (2020-09-22)

  • 修复了Safari v14中剪辑图像的错误

v1.12.2 (2020-08-10)

  • 更新的依赖项
  • 更新的视频工作者脚本
  • 略微更改的样式,应用于容器和视差图像

v1.12.1 (2020-04-29)

  • 已弃用Jarallax的Elements扩展。改为使用laxx
  • 增加了对视频错误事件的支持(错误时显示图像)
  • 为自托管视频添加了海报属性
  • 固定仅在禁用视差时播放视频背景
  • 删除了对rafl的依赖。改为使用本机requestAnimationFrame

v1.12.0 (2019-10-23)

  • 增加了对可滚动家长的支持
  • 删除了调整大小观察员支持(应该在没有它的情况下工作)

v1.11.1 (2019-10-01)

  • 修复了JS初始化中没有实际<img>标记的imgSrc选项

预览截图