jQuery视差滚动插件,(附原理分析,调用方法)
演示地址:http://www.jq22.com/jquery-info1799
jquery插件,使用验证过可用。
分析源代码后总结原理:设置background样式为fixed,判断浏览器滚动距离,当在视窗范围内时,调用$(window).scroll。并根据所设置的速度比例,进行同步滚动。
调用时,设置三个必要参数:data-speed,data-type,background样式为fixed
<div data-speed="4" data-type="background" style="background:url(...) 50% 0 no-repeat fixed">
js会根据data-speed来计算滚动速度,越小越快。
可以根据不同的data-type作不同处理,可自定义写代码,很方便。
background必须是fixed。
parallax.js源代码(需先引入jQuery)
$(document).ready(function(){ // Cache the Window object
$window = $(window); // Cache the Y offset and the speed of each sprite
$('[data-type]').each(function() {
$(this).data('offsetY', parseInt($(this).attr('data-offsetY')));
$(this).data('Xposition', $(this).attr('data-Xposition'));
$(this).data('speed', $(this).attr('data-speed'));
}); // For each element that has a data-type attribute
$('section[data-type="background"]').each(function(){ // Store some variables based on where we are
var $self = $(this),
offsetCoords = $self.offset(),
topOffset = offsetCoords.top; // When the window is scrolled...
$(window).scroll(function() { // If this section is in view
if ( ($window.scrollTop() + $window.height()) > (topOffset) &&
( (topOffset + $self.height()) > $window.scrollTop() ) ) { // Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -($window.scrollTop() / $self.data('speed')); // If this element has a Y offset then add it on
if ($self.data('offsetY')) {
yPos += $self.data('offsetY');
} // Put together our final background position
var coords = '50% '+ yPos + 'px'; // Move the background
$self.css({ backgroundPosition: coords }); // Check for other sprites in this section
$('[data-type="sprite"]', $self).each(function() { // Cache the sprite
var $sprite = $(this); // Use the same calculation to work out how far to scroll the sprite
var yPos = -($window.scrollTop() / $sprite.data('speed'));
var coords = $sprite.data('Xposition') + ' ' + (yPos + $sprite.data('offsetY')) + 'px'; $sprite.css({ backgroundPosition: coords }); }); // sprites // Check for any Videos that need scrolling
$('[data-type="video"]', $self).each(function() { // Cache the video
var $video = $(this); // There's some repetition going on here, so
// feel free to tidy this section up.
var yPos = -($window.scrollTop() / $video.data('speed'));
var coords = (yPos + $video.data('offsetY')) + 'px'; $video.css({ top: coords }); }); // video }; // in view }); // window scroll }); // each data-type }); // document ready
jQuery视差滚动插件,(附原理分析,调用方法)的更多相关文章
- 【精心推荐】12款很好用的 jQuery 图片滚动插件
这里收集了12款很好用的 jQuery 图片滚动插件分享给大家.jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各 ...
- Flexslider - 响应式的 jQuery 内容滚动插件
FlexSlider 是一款轻量的响应式 jQuery 内容滚动插件,能够帮助你在项目轻松的创建漂亮的内容滚动效果.这款插件曾经连续多年入选 WDL 的年度最佳 jQuery 插件,值得大家在网站开发 ...
- 10款很好用的 jQuery 图片滚动插件
jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各种很酷的图片效果,它可以让的网站更具吸引力.这里收集了10款很好 ...
- 16个最棒的jQuery视差滚动效果教程
今天我们整理了16个最棒的jQuery视差滚动效果教程 , 并附有演示地址,你可以快速的查看教程的效果,或者应用在自己的项目中.希望本文对想要学习或者使用jQuery视差效果的朋友有帮助,慢慢的欣赏吧 ...
- liMarquee – jQuery无缝滚动插件(制作跑马灯效果)
liMarquee 是一款基于 jQuery 的无缝滚动插件,类似于 HTML 的 marquee 标签,但比 marquee 更强大.它可以应用于任何 Web 元素,包括文字.图像.表格.表单等元素 ...
- (转)15个非常棒的jQuery无限滚动插件【瀑布流效果】
原文地址:http://www.cnblogs.com/lyw0301/archive/2013/06/19/3145084.html 现在,最热门的网站分页趋势之一是jQuery的无限滚动(也即瀑布 ...
- 利用jQuery无缝滚动插件liMarquee实现图片(链接)和文字(链接)向右无缝滚动(兼容ie7+)
像新闻类的版块经常要求一条条的新闻滚动出现,要实现这种效果,可以使用jQuery无缝滚动插件liMarquee. 注意: 1. 它的兼容性是IE7+,及现代浏览器. 2. 引用的jquery的版本最好 ...
- jquery垂直滚动插件一个参数用于设置速度,兼容ie6
利用外层的块级元素负外边距来滚动 1.使用 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ww ...
- 15个非常棒的jQuery无限滚动插件【瀑布流效果】
现在,最热门的网站分页趋势之一是jQuery的无限滚动(也即瀑布流).如果你碰巧观察Pinterest的网站,如Facebook,Twitter和deviantART的,你会发现无限滚动的动作,当旧的 ...
随机推荐
- SSH加固
1.修改ssh默认端口 vi /etc/ssh/sshd_config 中Port:service ssh restart 2.安装denyhosts,应对暴力破解ssh. A.直接 apt-get ...
- IO(七)----转换流
输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥梁. 输出字节流的转换流:OutputStreamWriter 可以把输出字节流转换成输出字符流 . 是符流通向字节流 ...
- OrchardNoCMS模块生成工具命令简化
OrchardNoCMS模块生成工具命令行简化列表: 目前只有codegen feature和cultures三个命令. 对应的都进行了参数简化. 例如:codegen module 简化为cod ...
- 【JavaScript】图片上传预览
上传文件实时显示[一张图片]: 个人理解:给img的src传值:这个值就是input[type='file']的value: 不过你要判断浏览器类型[很多]:IE6.0,IE7/8/9,Fixfox7 ...
- Redis 学习(二)
Redis可以存储以下5种数据类型 1. String 字符串 整数 浮点 2. List 一个链表 3. Set 无序收集器 4. Hash 无序散列表 5. Zset 有序集合
- MVC架构模式分析与设计(一)---简单的mvc架构
首先 我要感谢慕课网的老师提供视频资料 http://www.imooc.com/learn/69 下面我来进行笔记 我们制作一个简单的mvc架构 制作第一个控制器 testController.cl ...
- C++ URLDecode和URLEncode实现——仅限gb2312,非utf8
转载--http://blog.163.com/zhangjie_0303/blog/static/9908270620148251658993/ #include <iostream> ...
- jdbc-批量插入、批量删除、批量更新
一.JDBC的批量插入 JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等. 我用Mysql5.1.5的JDBC driver 分别对三种比较常用的方法做了测试 方法 ...
- HTML+CSS知识点总结
转自:http://blog.csdn.net/qiushi_1990/article/details/40260447?utm_source=tuicool&utm_medium=refer ...
- js 数据类型 typeof的测试
, t2 = ', t3 = null, t4 = NaN, t5 = undefined, t6 = function() {}, t7 = true, t8 = window, t9 = docu ...