演示地址: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视差滚动插件,(附原理分析,调用方法)的更多相关文章

  1. 【精心推荐】12款很好用的 jQuery 图片滚动插件

    这里收集了12款很好用的 jQuery 图片滚动插件分享给大家.jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各 ...

  2. Flexslider - 响应式的 jQuery 内容滚动插件

    FlexSlider 是一款轻量的响应式 jQuery 内容滚动插件,能够帮助你在项目轻松的创建漂亮的内容滚动效果.这款插件曾经连续多年入选 WDL 的年度最佳 jQuery 插件,值得大家在网站开发 ...

  3. 10款很好用的 jQuery 图片滚动插件

    jQuery 作为最流行的 JavaScript 框架,使用简单灵活,同时还有许多优秀的插件可供使用.其中最令人印象深刻的应用之一就是各种很酷的图片效果,它可以让的网站更具吸引力.这里收集了10款很好 ...

  4. 16个最棒的jQuery视差滚动效果教程

    今天我们整理了16个最棒的jQuery视差滚动效果教程 , 并附有演示地址,你可以快速的查看教程的效果,或者应用在自己的项目中.希望本文对想要学习或者使用jQuery视差效果的朋友有帮助,慢慢的欣赏吧 ...

  5. liMarquee – jQuery无缝滚动插件(制作跑马灯效果)

    liMarquee 是一款基于 jQuery 的无缝滚动插件,类似于 HTML 的 marquee 标签,但比 marquee 更强大.它可以应用于任何 Web 元素,包括文字.图像.表格.表单等元素 ...

  6. (转)15个非常棒的jQuery无限滚动插件【瀑布流效果】

    原文地址:http://www.cnblogs.com/lyw0301/archive/2013/06/19/3145084.html 现在,最热门的网站分页趋势之一是jQuery的无限滚动(也即瀑布 ...

  7. 利用jQuery无缝滚动插件liMarquee实现图片(链接)和文字(链接)向右无缝滚动(兼容ie7+)

    像新闻类的版块经常要求一条条的新闻滚动出现,要实现这种效果,可以使用jQuery无缝滚动插件liMarquee. 注意: 1. 它的兼容性是IE7+,及现代浏览器. 2. 引用的jquery的版本最好 ...

  8. jquery垂直滚动插件一个参数用于设置速度,兼容ie6

    利用外层的块级元素负外边距来滚动 1.使用 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://ww ...

  9. 15个非常棒的jQuery无限滚动插件【瀑布流效果】

    现在,最热门的网站分页趋势之一是jQuery的无限滚动(也即瀑布流).如果你碰巧观察Pinterest的网站,如Facebook,Twitter和deviantART的,你会发现无限滚动的动作,当旧的 ...

随机推荐

  1. jquery easyui 1.4.1 验证时tooltip 的位置调整

    现象是在表单中如果显示两列控件,右边的控件是combo,combobox 等右边有按钮的,宽度为100%时,验证不通过的tooltip 显示位置不准确如下图所示 打开 jquery.easyui-1. ...

  2. MVC模式在UI里的应用

    In a similar way to other parts of a game, user interfaces usually go through several iterations unt ...

  3. js 获取时间间隔

    现在感觉sublime   IDE 用着比较方便,也比较美观,不知道大家用的是啥ide.

  4. Java中getResourceAsStream的用法

    首先,Java中的getResourceAsStream有以下几种: 1. Class.getResourceAsStream(String path) : path 不以'/'开头时默认是从此类所在 ...

  5. 【UOJ #13】【UER #1】跳蚤OS

    http://uoj.ac/problem/13 建立trie树,然后建立go指针, 和AC自动机里的fail指针差不多, 走到一个快捷方式就从go指针走. 注意在trie树上要保留字符'/',不能用 ...

  6. C#-WinForm-MDI窗体容器、权限设置

    MDI窗体容器 - 放窗体的容器 窗体时顶级控件,是不允许放到其他的控件或窗体中的 (李献策lxc) 窗体属性中有一个属性:IsMdiContainer - 确定该窗体是否是MDI容器 在窗体中放一个 ...

  7. javascript学习笔记10----字符串的基本操作

    1.字符串的基本操作如下: 定义字符串: var str = "Hello World!" 字符串的基本操作如下: str.length-----返回字符串长度,这里返回12 st ...

  8. DialogFragment is gone after returning back from another activity

    基本情景如下: 在DialogFragment中单击一个按钮跳转到another Activity做一些逻辑处理,然后将返回的结果回显到该DialogFragment上. 处理逻辑是: 在Dialog ...

  9. RabbitMQ consumer的一些坑

    坑 坑就像是恶梦,总是在最不设防的时候出现,打的你满地找牙.这里记录一些坑,遇到的朋友可以及时的跳出,避免带来损失. 使用事件方式去获取queue中的消息,然后再进行处理.这看起来没什么问题,但是如果 ...

  10. mybatis一个怪异的问题: Invalid bound statement not found

    ssm中报一下错误: invalid bound statement (not found): me.tspace.pm.dao.userdao.getuser    at org.apache.ib ...