- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个图片列表,我想在向下/向上滚动的同时缓慢向上/向下移动图片。
图像本身由“object-fit: cover;”设置和“对象位置:中心 50%;”,所以我有空间将图像向下移动到“对象位置:中心 100%;”的最大值向上滚动时。相反,将图像向上移动到“object-position:center 0;”的最小值。向下滚动时。
简而言之:当用户向下滚动图片列表时,每张图片应该从其隐藏的顶部慢慢显示更多内容,反之亦然。
我在 whatsapp 客户端 (ios) 上发现了这个不错的视差效果。当用户向下滚动时,媒体内容会向上滚动一点,反之亦然。我想达到完全一样的效果。
所以我编写了以下代码 - 它可以工作,但是性能很差,我正在寻找更好的解决方案。有谁知道这个问题的更好解决方案?也许通过使用“纯 css parralax”(http://keithclark.co.uk/articles/pure-css-parallax-websites/)。这将是最好的解决方案 - 但是,我不能对我的问题采用这种技术。
您可以将它复制/粘贴到一个新的 index.html 中,它会按原样工作。
这是完整的工作代码(性能不佳):
(function() {
document.addEventListener('DOMContentLoaded', function() {
/** returns true, if node is in viewport.
*/
function node_is_in_viewport(element) {
var rect = element.getBoundingClientRect();
var html = document.documentElement;
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || html.clientHeight) &&
rect.right <= (window.innerWidth || html.clientWidth)
);
}
/** returns "y-value" of css-property "object-position"
*/
function getObjectPosY(translateString){
var n = translateString.indexOf('%');
var n1 = translateString.lastIndexOf("%");
var res = parseInt(translateString.slice(n+1,n1));
return res;
}
var list_img = document.querySelector('.list-img');
var pos_list = list_img.scrollTop;
var all_imgs = document.querySelectorAll('.list-img>li img');
console.log('actual position of the list:');
console.log(pos_list);
list_img.addEventListener('scroll', function(e){
var scroll = this.scrollTop;
// positive number means "scroll down"
// negative number means "scroll up"
var offsetScrollTreshold = scroll - pos_list; // unused
// move image 1 unit up/down
var offsetScroll = 3;
var speed = 1;
console.log('position of list (pos_list):');
console.log(pos_list);
console.log('scroll:');
console.log(scroll);
console.log('offset (scroll - pos_list):');
console.log(offsetScrollTreshold);
if(scroll > pos_list) {
console.log('User is scrolling DOWN');
console.log('I want to show more from the hidden top of all the images within the actual viewport while user is scrolling down. But each image should not exceed its original image size.');
list_img.classList.add('scroll-down');
list_img.classList.remove('scroll-up');
[].forEach.call(all_imgs, function(node) {
var node_in_view = node_is_in_viewport(node);
// if image is in viewport move it up to "make a parallax effect"
if(node_in_view){
console.log('-----');
console.log('actual img to move up (new Y must be lower):');
console.log(node.src);
var old_y = 0;
var theCSSprop = window.getComputedStyle(node, null).getPropertyValue('object-position');
if( theCSSprop.indexOf('%')!=-1 ){
console.log(theCSSprop);
// returns the actual "y"-position of the image
old_y = getObjectPosY(theCSSprop);
console.log('old_y:');
console.log(old_y);
}
// User is scrolling down, so I want to view more from the "top of the image" which is actually a little hidden (because of object-fit: center 50%)
// to view more from the top of the image, I have to set the y-value (actually: 50%) lower, but not more than 0.
if(old_y > 0 && old_y <= 100){
console.log('scroll image "slowly" up while user is scrolling down (parallax effect)');
var new_y = old_y - offsetScroll * speed;
if(new_y<0){
new_y = 0;
}
console.log('new_y:');
console.log(new_y);
node.style.objectPosition = '50% '+ new_y + '%';
}
}
});
}
else {
console.log('User is scrolling UP');
console.log('I want to show more from the hidden bottom of all the images within the actual viewport while user is scrolling up. But each image should not exceed its original image size.');
list_img.classList.remove('scroll-down');
list_img.classList.add('scroll-up');
[].forEach.call(all_imgs, function(node) {
var node_in_view = node_is_in_viewport(node);
// if image is in viewport move it down to "make a parallax effect"
if(node_in_view){
console.log('actual img to move down (new Y must be higher):');
console.log(node.src);
var theCSSprop = window.getComputedStyle(node, null).getPropertyValue('object-position');
if(theCSSprop.indexOf('%')!=-1){
console.log(theCSSprop);
// returns the actual "y"-position of the image
old_y = getObjectPosY(theCSSprop);
console.log('old_y:');
console.log(old_y);
}
// User is scrolling up, so I want to view more from the "bottom of the image" which is actually a little hidden (because of object-fit: center 50%)
// to view more from the bottom of the image, I have to set the y-value (actually: 50%) higher, but not more than 100%.
if(old_y >= 0 && old_y < 100){
console.log('scroll image "slowly" down while user is scrolling up (parallax effect)');
var new_y = old_y + offsetScroll * speed;
if(new_y>100){
new_y = 100;
}
console.log('new_y:');
console.log(new_y);
node.style.objectPosition = '50% '+ new_y + '%';
}
}
});
}
pos_list = scroll;
console.log('pos_list:');
console.log(pos_list);
});/*end eventlistener scroll */
}); /*end .DOMContentLoaded */
}());
body, html, #ctn_infinity, .list-img{
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
.list-img{
height: 90vh;
overflow: auto;
}
.list-img{
list-style: none;
width: 400px;
}
.list-img>li{
display: flex;
flex-direction: column;
border: 2px solid black;
margin: 16px auto;
}
.list-img div{
background: yellow;
}
.list-img img{
object-fit: cover;
width: 100%;
/* original image size: 400 width x 200 height */
/* I use object position center: when scrolling up or down, images should "slowly" show its hidden part of its images (max. 25px top or 25px down) */
height: 150px;
/* Unfortunately, I cannot use "transform: translateY(-5px);", because this will move the whole img-container instead of the img itself up/down" */
object-position: center 50%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML-TEST</title>
</head>
<body>
<div id="ctn_infinity">
<ul class="list-img">
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
<li><img src="http://lorempixel.com/400/200">
<div>lorem epsum..</div></li>
</ul>
</div>
</body>
</html>
最佳答案
我在 csstriggers.com 上找不到对象位置。但是我自己检查了一下,一旦我用 object-fit: cover 改变了一个元素的对象位置,我就得到了一个Recalculate style
和一个Paint
。
如果您不熟悉这些,这些是浏览器在特定事件后更新屏幕的方式。而且你的代码触发的事件非常繁重。现在,在每次滚动事件中,您的代码都会在浏览器窗口上触发昂贵的绘制事件。
基本上您要做的是重构它,这样您的代码就不必重新绘制。您可能希望将 objectPosition 更改为 .style.transorm = "translate3d(0, "+ new_y + ", 0)";
。那些不应该像现在这样触发绘画事件。
尽管如此,它仍然有可能触发绘画。我建议您阅读有关滚动性能的内容,因为关于堆栈溢出的解释太多了,Paul Lewis wrote一篇关于此的好文章。
关于javascript - 滚动时垂直 img-list 的有效 "parallax effect",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42249364/
我想对齐输入,它有两个标签和一个没有任何标签的按钮,以便它们都在同一级别。 这是期望的效果: 这是我到目前为止得到的: 我通过在按钮前添加带有空白空间的标签,设法让它看起来像我想要的样子,但这并不理想
您好,我有一个关于 JavaFX 的问题。我担心答案是剥皮,我对此一无所知,但这里是。 我想在 JavaFX 中制作 Accordion/TabPane 交叉。我将尝试用文字解释自己,但在下面我已经包
这可能被认为是一个低俗的问题。然而,我还没有找到任何代码,也没有找到任何讨论如何用我的编程语言 C 来解决这个问题的论坛。已经进行了大量的尝试,并且最终都以实际上“硬编码”新数组而告终。 我正在尝试垂
我有以下代码: public Frame() { super(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT
是否可以将 UIModalTransitionStyleFlipHorizontal 修改为垂直而不是水平? 我通常会使用 CATransition 来执行此操作,但该方法需要将一个 UIView
我有一个 ios5 导航的想法,我正在一个应用程序上做,我认为从 SOF 那里得到一些关于我的想法的建设性批评是明智的。 p> 想法: UIView 包含 6 个左右垂直堆叠的按钮 UIButtons
我正在努力进行一些 iOS 开发,我希望有人能够帮助我提出建议。我想实现一堆看起来像一副纸牌的 UIView。用户应该能够通过触摸刷出“卡片”。我想到了带有 pagingEnabled 的 UIScr
我想创建一个包含四张图片的入口页面作为指向其他页面的链接。但我希望这些图片显示在显示器的中间(垂直和水平)。我已经尝试了很多事情,但我做对了。我什至无法以任何方式垂直对齐图片(例如底部,您将在代码中看
我有代码在 div 的底部创建一个元素作为向下的“箭头/指针”。 我需要这样写,以便“箭头/指针”指向左侧 当前代码 - HTML 掩码 css 在 fiddle 中 https://j
下面的代码将焦点放在中间的 div 上,但该 div 出现在窗口的底部。如何使该 div 居中,使其显示在窗口的中心[垂直],而不设置固定位置。 $(function(){ $("#focus
这个问题在这里已经有了答案: Vertical Align Center in Bootstrap 4 [duplicate] (20 个答案) 关闭 4 年前。 我创建了一个由两列组成的结构,第一
演示:http://jsfiddle.net/vpH8r/2/ 我在上面创建了一个 jfiddle,试图让垂直滑动正常工作。 当前的问题是 slider 标签不会移动,并且不会出现从顶部到底部的干净滑
我正在尝试在 javascript 中创建一个垂直 slider 小部件。我不是在寻找任何插件或库,我正在尝试查看如何使用纯 javascript 完成它。我想我大概明白了。 请在此处查看到目前为止已
我正在尝试创建一个响应式导航列表,并将图像置于页面列表的中心 我已经设法接近那个,但是图像比列表更靠前。 我想像这样得到它,但似乎无法弄清楚。 任何帮助将不胜感激! 代码:https://jsfidd
有人可以帮我将导航栏居中,使其在页面上垂直居中吗? 我希望导航栏中央的链接位于顶部,而不是像现在这样向左浮动。 提前致谢 Toggle navigation
我想垂直、水平转换图像,并使用 javascript 缩放图像。目前我设法使调整大小的图像起作用,但它看起来不正确。我正在尝试使用类似于 Transform image 的东西.调整垂直移动条时,图像
这可能是一个问题,但要求太多,但我已经达到了我的极限。 我有这段 ( http://jsfiddle.net/3whTa/) CSS,它创建了一个箭头水平面包屑导航。 我想做的是将其转换为垂直。就像箭
我正在使用 opencv 进行非常简单的操作,但我无法理解为什么会出现此错误/问题。图像被操作到图像的一半(垂直)。 Mat img = imread("/Users/tanmoy/Documents
我想知道是否有人有任何技术来定位 css 生成的内容。例如: .block { height: 150px; width: 150px; border: 1px solid black;
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭3年前。 Improve this que
我是一名优秀的程序员,十分优秀!