- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我喜欢这个网站中使用的 JS 视差技术 https://www.beamland.com/
基于滚动设置的div,更改css VH,显示下面的内容。
我正在尝试重现类似的内容,但我无法获得计算可见屏幕高度与滚动高度以及文档整个高度的公式。
所以我在该网站的底层进行了挖掘,但我不明白正在进行什么样的计算才能达到效果。
BEAM.initParallax = function() {
function a() {
var a = q - 1,
b = a / j,
c = Math.ceil(b),
d = 100 - a % j / j * 100 + "vh",
e = 100 * b + 4e3 / j + "vh";
r = !1, "Mobile Safari" !== h.browser.name && "Android" !== h.os.name || (e = a + 30 + "px"), c < 1 && (c = 1), a % j === 0 && a > 0 && c++;
for (var f = 0; f < m.length; f++) f + 1 > c ? m[f].style.height = "100vh" : f - 1 < c && (m[f].style.height = "0vh");
m[c - 1] && (m[c - 1].style.height = d), o.removeClass("is-active"), $(o[c - 1]).addClass("is-active"), b < s ? (l.removeAttr("style").addClass("stuck"), n.removeClass("faded")) : l[0].hasAttribute("style") || (n.addClass("faded"), l.removeClass("stuck").css("top", e))
}
function b() {
if (s = 3.887, k <= 1024) {
s = 3.915;
var a = Math.abs(j - document.getElementsByClassName("Parallax-spacer")[0].style.height);
$(".Parallax-spacer").css("height", j + "px"), a > 20 && Math.ceil((q - 1) / j) >= 4 && (p < q && (a *= -1), window.scrollTo(0, q - 4 * a))
}
}
function c() {
return "Android" === h.os.name ? i.outerHeight() : i.innerHeight()
}
function d() {
return "Android" === h.os.name ? i.outerWidth() : i.outerWidth()
}
function e() {
p = q, q = window.scrollY, f()
}
function f() {
r || window.requestAnimationFrame(a), r = !0
}
if ($(".Parallax-Hero").length) {
var g = new UAParser,
h = g.getResult(),
i = $(window),
j = c(h),
k = d(h),
l = $("div.Nav-Main"),
m = $(".Parallax-panel"),
n = $(".Parallax-wayfinder"),
o = n.find(".Parallax-pagination--dot"),
p = 0,
q = 0,
r = !1,
s = 0;
b(), $(".Parallax-pagination--dot").on("mouseup touchend", function(a) {
a.preventDefault();
var b = $(".Parallax-pagination--dot").index(this),
c = b * j + 1;
$("html, body").animate({
scrollTop: c + "px"
}, 500)
}), i.on("scroll", function() {
e()
}), i.on("resize", function() {
j = c(h), k = d(h), b(), e()
}), window.requestAnimationFrame(a)
}
我什至在codepen上查看了各种其他视差和代码效果,但我没有找到与此效果类似的东西来理解计算。有人可以帮我揭开其中的逻辑吗?谢谢
最佳答案
这是一个缩小的代码。出于开发目的,您最好重命名变量,以便于阅读。
m = $(".Parallax-panel"),
becomes:
parallaxPanel = $(".Parallax-panel"),
then
m.length
is
parallaxPanel.length
q = window.scrollY
becomes
windowScrollY = window.scrollY
then
a = windowScrollY - 1;
j = c(h),
becomes
windowHeight = c(h),
试试这个广告,看看您是否能更好地理解。
更新:
我建议这种命名约定的原因是为了让您更好地理解这些计算。
b = a / j;
这还不清楚,但是:
b = (windowScrollY - 1) / windowHeight;
更明显。 window.ScrollY 是文档当前从原点垂直滚动的像素数。 window.outerHeight 是窗口的高度。
c = Math.ceil(b);
b 是 float ,所以现在 c 是整数。
d = 100 - a % j / j * 100 + "vh";
d = 100 - (windowScrollY - 1) % windowHeight / windowHeight * 100 + "vh";
这给出了滚动的百分比。
我无法为您全部解码。您应该具备数学和编程知识才能做到这一点。
关于javascript - 解构这个视差的js公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48921600/
我在寻找有关 JavaScript 解构的信息,并在 Packt Publication 的视频系列中找到了视频“解构赋值”。在视频的一开始,我看到了以下代码: var [a, b] = [1,2,3
知识渊博的人可以帮助我理解 Scalaz 的工作原理吗?和 co.有效吗?我是 Scalaz 的新手,有点迷失在探索中。 我想做的是在 List 中累积错误,例如 (v0 v1) foldLeft
Haskell 中的模式匹配可以用这种方式解构数字吗: f (n + 1) = n 我期望 ex 的前身:f 6 = 5、f 5 = 4 等。 我在这里找到了这种模式匹配用法: https://wik
我有一个与此类似的文件: const COLORS = { PRIMARY_COLOR: 'red', SECONDARY_COLOR: 'green' }; const APP = {
作为对 SO 问题的回答,我构建了一个循环函数,并构建了我迄今为止最复杂的解构,这奇迹般地起作用了: (defn fib? [a b & [c & r]] (if (= c (+ a b))
我对破坏感到困惑。 我在其中使用了 React,我们这样做了 const [ books, setBooks ] = useState([{a:'v'}] 和 const {books} = useC
我确定这在某个地方得到了回答,但我缺乏制定搜索的词汇。 #include class Thing { public: int value; Thing(); virtual ~Thing()
如果我在 javascript 中有这样一个对象: let obj = {b: 3, c: 4, d: 6} 如果我解构它,我可以很容易地得到不同的部分,例如,如果我这样做,我可以得到 c 和 d:
这个问题在这里已经有了答案: One-liner to take some properties from object in ES 6 (11 个回答) 2年前关闭。 我似乎不记得如何写这个解构模式
我有一个函数可以查询我的数据库中最近的 X 个条目,它返回一个 map 向量,如下所示: [{:itemID "item1" :category "stuff" :price 5} {:itemI
根据所选语言,我需要销毁对象并获得所需的值。 我该怎么做才能不破坏整个对象? const translate = { "navMenu1": { "en": "Menu 1",
我试图理解这种 ES6 解构。有人可以解释这行代码将编译成什么吗? const { loading, route: { pageName = 'default' } = {} } = this.pro
我有一个程序,可以输出这样的一些条件(这是实际输出,它是伪代码): if ( first occurance of 'AB' -0.5 ) * (( number of products viewe
Serilog的@的目的是什么?句法? 如果我运行以下命令: var dummy = new { Foo = "Bar", Date = DateTime.Now }; Log.Information
JSON 编码的数组从 PHP 传递到 HTML 文档。目前还不清楚如何将该数组解构为 javascript 可用的片段。例如,考虑以下 HTML: {"foo":[{"id":1},{"id":3}
我正在 Chrome 的控制台选项卡中尝试使用以下代码进行 JavaScript 解构,这给了我未捕获的语法错误:标识符“a”已被声明异常 o = { a: "foo", b: 12, c: "bar
我有一个 JavaSCript 对象 person,其中包含 id、name、phone 和 地址属性。我想修改属性名称并将它们放入新对象 personData 中。这可以一步完成吗?: 第 1 步(
有没有办法从 WPF 中获取 Geometry 实例的内部结构? 我需要转换一串用户输入的几何数据,例如 M10,100 C10,300 300,-200 300,100 Z 用于分离几何命令(移动、
我的代码中有一个 promise : req.getValidationResult() .then(result => { let errors = resu
我正在为 Apollo Client 生成 Flow 类型,我目前有这个: type FetchModuleQuery = {| // Fetch single module module:
我是一名优秀的程序员,十分优秀!