- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个包装器,其中我在 their Airpods Pro page 上设置了与 Apple 相同的动画效果。 。它基本上是一个视频,当我滚动时,视频一点一点地播放。视频的位置是固定的,因此文本可以很好地在其上滚动。但是,文本仅在特定分区的偏移量(文本显示)之间时可见。
那部分工作正常。现在我希望当用户滚动到视频末尾并因此完成动画时,视频效果包装器从固定位置变为相对位置。这样该网站就会在视频动画之后正常滚动其内容。
这是我已经尝试过的示例:
//If video-animation ended: Make position of video-wrapper relative to continue scrolling
if ($(window).scrollTop() >= $("#video-effect-wrapper").height()) {
$(video).css("position", "relative");
$("#video-effect-wrapper .text").css("display", "none");
}
这种方法可行...但除了流畅之外一切都很顺利。并且还需要能够向后滚动网页。
我在尝试解决此问题时遇到的问题:
最佳答案
在 Airpods Pro page 上进行一些逆向工程时,我们注意到动画不使用 video
,而是使用 canvas
。实现如下:
HTMLImageElement
的形式创建图像数组scroll
DOM 事件使用react,并请求与最近的图像相对应的动画帧,requestAnimationFrame
ctx.drawImage
显示图像(ctx
是 的 2d
上下文) Canvas
元素)requestAnimationFrame
函数应该可以帮助您实现更平滑的效果,因为帧将被延迟并与目标屏幕的“每秒帧数”速率同步。
有关如何在滚动事件上正确显示框架的更多信息,您可以阅读:https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event
话虽如此,关于您的主要问题,我有一个可行的解决方案,其中包括:
video
元素相同。其目的是避免设置为绝对
位置时视频与 HTML 的其余部分重叠scroll
事件回调,当占位符到达视口(viewport)顶部时,将视频的位置设置为absolute
,右侧top
值这个想法是,视频始终保持在流之外,并在滚动到底部时在正确的时刻出现在占位符上。
这是 JavaScript:
//Get video element
let video = $("#video-effect-wrapper video").get(0);
video.pause();
let topOffset;
$(window).resize(onResize);
function computeVideoSizeAndPosition() {
const { width, height } = video.getBoundingClientRect();
const videoPlaceholder = $("#video-placeholder");
videoPlaceholder.css("width", width);
videoPlaceholder.css("height", height);
topOffset = videoPlaceholder.position().top;
}
function updateVideoPosition() {
if ($(window).scrollTop() >= topOffset) {
$(video).css("position", "absolute");
$(video).css("left", "0px");
$(video).css("top", topOffset);
} else {
$(video).css("position", "fixed");
$(video).css("left", "0px");
$(video).css("top", "0px");
}
}
function onResize() {
computeVideoSizeAndPosition();
updateVideoPosition();
}
onResize();
//Initialize video effect wrapper
$(document).ready(function () {
//If .first text-element is set, place it in bottom of
//text-display
if ($("#video-effect-wrapper .text.first").length) {
//Get text-display position properties
let textDisplay = $("#video-effect-wrapper #text-display");
let textDisplayPosition = textDisplay.offset().top;
let textDisplayHeight = textDisplay.height();
let textDisplayBottom = textDisplayPosition + textDisplayHeight;
//Get .text.first positions
let firstText = $("#video-effect-wrapper .text.first");
let firstTextHeight = firstText.height();
let startPositionOfFirstText = textDisplayBottom - firstTextHeight + 50;
//Set start position of .text.first
firstText.css("margin-top", startPositionOfFirstText);
}
});
//Code to launch video-effect when user scrolls
$(document).scroll(function () {
//Calculate amount of pixels there is scrolled in the video-effect-wrapper
let n = $(window).scrollTop() - $("#video-effect-wrapper").offset().top + 408;
n = n < 0 ? 0 : n;
//If .text.first is set, we need to calculate one less text-box
let x = $("#video-effect-wrapper .text.first").length == 0 ? 0 : 1;
//Calculate how many percent of the video-effect-wrapper is currenlty scrolled
let percentage = n / ($(".text").eq(1).outerHeight(true) * ($("#video-effect-wrapper .text").length - x)) * 100;
//console.log(percentage);
//console.log(percentage);
//Get duration of video
let duration = video.duration;
//Calculate to which second in video we need to go
let skipTo = duration / 100 * percentage;
//console.log(skipTo);
//Skip to specified second
video.currentTime = skipTo;
//Only allow text-elements to be visible inside text-display
let textDisplay = $("#video-effect-wrapper #text-display");
let textDisplayHeight = textDisplay.height();
let textDisplayTop = textDisplay.offset().top;
let textDisplayBottom = textDisplayTop + textDisplayHeight;
$("#video-effect-wrapper .text").each(function (i) {
let text = $(this);
if (text.offset().top < textDisplayBottom && text.offset().top > textDisplayTop) {
let textProgressPoint = textDisplayTop + (textDisplayHeight / 2);
let textScrollProgressInPx = Math.abs(text.offset().top - textProgressPoint - textDisplayHeight / 2);
textScrollProgressInPx = textScrollProgressInPx <= 0 ? 0 : textScrollProgressInPx;
let textScrollProgressInPerc = textScrollProgressInPx / (textDisplayHeight / 2) * 100;
//console.log(textScrollProgressInPerc);
if (text.hasClass("first"))
textScrollProgressInPerc = 100;
text.css("opacity", textScrollProgressInPerc / 100);
} else {
text.css("transition", "0.5s ease");
text.css("opacity", "0");
}
});
updateVideoPosition();
});
这是 HTML:
<div id="video-effect-wrapper">
<video muted autoplay>
<source src="https://ndvibes.com/test/video/video.mp4" type="video/mp4" id="video">
</video>
<div id="text-display"/>
<div class="text first">
Scroll down to test this little demo
</div>
<div class="text">
Still a lot to improve
</div>
<div class="text">
So please help me
</div>
<div class="text">
Thanks! :D
</div>
</div>
<div id="video-placeholder">
</div>
<div id="other-parts-of-website">
<p>
Normal scroll behaviour wanted.
</p>
<p>
Normal scroll behaviour wanted.
</p>
<p>
Normal scroll behaviour wanted.
</p>
<p>
Normal scroll behaviour wanted.
</p>
<p>
Normal scroll behaviour wanted.
</p>
<p>
Normal scroll behaviour wanted.
</p>
</div>
您可以在这里尝试:https://jsfiddle.net/crkj1m0v/3/
关于javascript - 滚动时元素从固定到相对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59866246/
我们有一个应用程序,我们可以在其中设计标签,然后根据设计创建图像并将其发送到打印机。图像在通过 TCP/IP 发送之前被制成字节流。 简而言之,我们有一个旋转设置,可以自动计算标签上不同元素的位置。而
我们在 IIS7 中有一个 MVC3 应用程序:http://mydomain.com/myapplication/ javascript 中的相对 URL 是什么: http://mydomain.
我的例子:http://jsfiddle.net/kwnk8qup/ 我的代码: container(父 div
如何将元素放置在右侧,并保持后面元素的持久位置? 我想在没有 Efficiently ... 的情况下将 text-align: right 对齐到右侧,并保留 t2 和 t3 元素之间的空间。 当我
假设我有一个包含以下文件的模块包。一个空文件 C:\codes\package\__init__.py 和一些重要文件: 一个位于C:\codes\package\first.py def f():
我正在尝试使用以下代码在屏幕上相对移动光标: input.type = INPUT_MOUSE; ZeroMemory(&input, sizeof(input)); input.mi.mouseDa
我排列了 3 个 div;中心 div 有许多 position:absolute 图像(它们重叠——一次显示一个图像;其余的是 display:none;对于 jQuery 交叉淡入淡出,这不是密切
我在 SQL 2000 数据库中有以下简化的表结构: ID AppName Key Value EffectiveDate -- ------- ----- ------- ---
给定以下配置: server { listen 80; server_name site.com; location /proxy { proxy_pa
我正在使用这些方法动态加载图像和资源目录,但它们并不适用于所有情况 new Uri(@"pack://application:,,/Images/lession_tab.png"); 此方法不适用于图
在插入/更新许多行时,我知道SQLite的“问题”,但事实并非如此。 我正在更新包含约250条记录的表中的ONE一行(由PK索引)中的ONE字段。查询通常需要200毫秒左右的时间。听起来很少,但很大。
如何向左或向右滑动线性布局。在该线性布局中,默认情况下我有一个不可见的删除按钮,还有一些其他小部件,它们都是可见状态,当向左滑动线性布局时,我需要使其可见的删除按钮,当向右滑动时,我需要隐藏该删除按钮
Imagen 我有一个 2D 点 (x,y) 列表,它们在我的简单游戏中描述了 2D 地形。 然后我让 glVertex() 在 GL_POINTS 模式下绘制所有这些点。 然后我有一个球,它也有它的
我正在使用 Google CloudSQL 并具有以下简单代码: ;dbname=', 'root', '' ); $db->setAttribute ( PDO::ATTR_ERRMODE, PDO
我知道有几个类似的问题,但是,其中的示例并没有说明问题,或者我无法从中获利 - 我真可耻。 所以我的问题是在带有 GUI 的简单应用程序中加载图像。例如: 我在 "D:\javaeclipseprog
我对放置在表格单元格内的 div 有疑问。单元格具有固定的高度和 div 相对于 height:100% 定位。 jsfiddle example td { height:80px;
我正在抓取一些数据。 我需要的数据点之一是日期,但包含此数据的表格单元格仅包括月份和日期。幸运的是,年份被用作对表格进行分类的标题元素。 出于某种原因,year = table.find_elemen
我想要一个 DIV,最大 90% 高度或 90% 宽度。我只想将图片作为背景放入 DIV 中,并且完整的 svg-image 应该是可见的。在移动设备和桌面设备上。 CSS: .camera {
我是网页设计新手,想了解 CSS 中定位的概念。我对相对和绝对定位元素有一些了解。在下面的 fiddle 中,我有一个 作为 的父级.当我做 p position:absolute ,边框崩溃不像我
我在调整图像高度相对于浏览器窗口的大小时遇到了一些严重的困难——宽度可以很好地调整大小,但高度保持不变。我尝试过 height: 100% 和 height: auto,以及 height: 10
我是一名优秀的程序员,十分优秀!