- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有 1 个固定的 DIV #scroll-up
,它是浏览器窗口高度的 100%。我在它下面还有另一个 DIV #next-prev
。 #next-prev
出现后,我希望 #scroll-up
DIV 开始向上滚动。我的代码 blow 就是这样做的,但是一旦 #next-prev
出现在 View 中,#scroll-up
就会无缘无故地跳起来。效果应该是无缝的。
如何防止跳转?
我已经在 jsFiddle 上设置了这个问题的演示:http://jsfiddle.net/sya0jr6p/1/
JS
使用 jQuery Visible plugin检测何时出现#next-prev。
$(window).scroll(function() {
if ($('#next-prev').visible(true)) {
console.log ( '#next-prev visible' );
// Initiate scroll up of '#scroll-up' when '#next-prev' is in the viewport
var $tagline = $('#scroll-up');
var windowScroll;
// Function
function slidingTitle() {
//Get scroll position of window
windowScroll = $(this).scrollTop();
$tagline.css({
'top' : -(windowScroll/3)+"px"
});
}
// Initiate
slidingTitle();
} else {
console.log ( '#next-prev not visible' );
// The element is NOT visible, do something else
}
});
更新
经过进一步测试,只有当 #nav-wrap
不在屏幕上时,跳转才会存在。如果它在屏幕上,则不会发生跳转并按预期工作。
我注意到的另一件事;当您第一次加载页面时,#nav-wrap
还没有出现在屏幕上。像往常一样向下滚动页面,直到您看到 #scroll-up
跳起来,如果您继续向下滚动,您就会看到我想要发生的事情。 #scroll-up
正常滚动。但是,如果在跳转发生后,您将页面向上滚动到最顶部,您会注意到 #scroll-up
的位置与您第一次加载页面时的位置不同。莫非计算错了?或者代码的顺序可能是错误的?只是一些想法...
更新
这是我希望滚动如何工作的动画 GIF,给出的动画答案并不直观:
最佳答案
您看到跳转的原因是,一旦 #next-prev
进入 View ,您只需将当前窗口滚动位置除以 3,并将其设置为 #scroll 的最高值-up
...当您到达#next-prev
时窗口滚动将大于 0,因此滚动除以 3 也将大于 0,并且 #向上滚动
将立即获得(大)负 top
值。
我不知道你从哪里得到除以三的除法 - 这会导致元素滚动变慢(又名。视差效果) - 这意味着即使没有跳跃和 #scroll-up
在正确的位置开始滚动,它仍然会覆盖三分之二的 #next-prev
一旦 #next-prev
完全可见......所以我不认为你真的想要视差效果......
我想你想要的是:#scroll-up
的top
值需要为零,直到#next-prev
的第一个像素滚动到 View 中,然后逐渐增长当 #next-prev
进入“更多” View 时,确保它恰好位于 #next-prev
之上...即。 #scroll-up
的顶部需要为 0 减去像素数如果 #next-prev
仍然完全低于视口(viewport)。
这是实现该目标的方法...
(function($) {
$(window).scroll(function() {
//Get the footer position relative to the document
var footer_offset = $("#footer").position().top;
//Get the window height (viewport height)
var window_height = $(window).height();
//Get the current scroll position
var window_scroll = $(window).scrollTop();
//Calculate the bottom of the window relative to the document
var window_bottom_offset = window_scroll + window_height;
//Calculate "how much" of the footer is above the bottom of the window
var footer_visible_pixels = window_bottom_offset - footer_offset;
//Default #semistuck's position to top: 0px
var semistuck_top = 0;
//But if the footer is above the bottom of the screen...
if (footer_visible_pixels > 0) {
//...move #semistuck up by the same amount of pixels
semistuck_top = -footer_visible_pixels;
}
//Actually move it
$("#semistuck").css('top', semistuck_top + 'px');
//Just to see each step of the calculation
console.log(footer_offset, window_height, window_scroll, window_bottom_offset, footer_visible_pixels, semistuck_top);
});
}(jQuery));
body,
html {
margin: 0;
}
#semistuck,
#loooooong,
#footer {
margin: 0;
padding: 4em 1em;
text-align: center;
box-sizing: border-box;
}
#semistuck {
position: fixed;
width: 50%;
height: 100%;
top: 0;
left: 0;
background: #afa;
}
#loooooong {
height: 2000px;
margin-left: 50%;
width: 50%;
background: repeating-linear-gradient(45deg, #606dbc, #606dbc 100px, #465298 100px, #465298 200px);
}
#footer {
background: #faa;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="semistuck">
This is stuck until the<br>footer scrolls into view
</div>
<div id="loooooong"></div>
<div id="footer">
When this gets into<br>view #semistuck will<br>get out of its way
</div>
显然上面的代码比需要的更冗长,但我觉得计算中非常小的增量使发生的事情更清楚......
下面是相同内容的简化形式:
var footer_visible_pixels = $(window).scrollTop() + $(window).height() - $("#footer").position().top;
$("#semistuck").css('top', (footer_visible_pixels > 0 ? -footer_visible_pixels : 0) + 'px');
...想想看,您实际上可能想要视差效果,但只是希望页脚位于顶部,所以这里有一个稍微修改过的版本,它提供了一些很酷的效果:
(function($) {
$(window).scroll(function() {
//Get the footer position relative to the document
var footer_offset = $("#footer").position().top;
//Get the window height (viewport height)
var window_height = $(window).height();
//Get the current scroll position
var window_scroll = $(window).scrollTop();
//Calculate the bottom of the window relative to the document
var window_bottom_offset = window_scroll + window_height;
//Calculate "how much" of the footer is above the bottom of the window
var footer_visible_pixels = window_bottom_offset - footer_offset;
//Default #semistuck's position to top: 0px
var semistuck_top = 0;
//But if the footer is above the bottom of the screen...
if (footer_visible_pixels > 0) {
//...move #semistuck up by the same amount of pixels
semistuck_top = -footer_visible_pixels/3;
}
//Actually move it
$("#semistuck").css('top', semistuck_top + 'px');
//Just to see each step of the calculation
console.log(footer_offset, window_height, window_scroll, window_bottom_offset, footer_visible_pixels, semistuck_top);
});
}(jQuery));
body,
html {
margin: 0;
}
#semistuck,
#loooooong,
#footer {
margin: 0;
padding: 4em 1em;
text-align: center;
box-sizing: border-box;
}
#semistuck {
position: fixed;
width: 50%;
height: 100%;
top: 0;
left: 0;
background: #afa;
}
#loooooong {
height: 2000px;
margin-left: 50%;
width: 50%;
background: repeating-linear-gradient(45deg, #606dbc, #606dbc 100px, #465298 100px, #465298 200px);
}
#footer {
background: #faa;
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="semistuck">
This is stuck until the<br>footer scrolls into view
</div>
<div id="loooooong"></div>
<div id="footer">
When this gets into<br>view #semistuck will<br>get out of its way
</div>
与原来的建议相比,只有两件事发生了变化:
position: relative
#semistuck
的负 top
除以三这些都不适用于 iPhone、iPad 以及可能大多数其他触摸设备。它们不会在 滚动期间触发任何滚动事件,而只会在 之后触发。因此,如果对移动设备的支持是一个问题,您应该检测到这些并提供回退 - 替代方案是仅在滚动结束后才会发生的非常跳跃的效果。据我所知,没有解决方法,根据具体情况,处理它的最佳方法可能是:
关于javascript - 滚动功能不会产生平滑过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31852308/
我用 geom_line 和 geom_ribbon 创建了一个图(图 1),结果还不错,但为了美观,我希望线条和丝带更平滑。我知道我可以使用 geom_smooth 作为线条(图 2),但我不确定是
我正在使用 Javascript 制作 HTML5 Canvas 游戏。 我想让一个物体平滑地转向某个方向。 我将方向存储为变量,并使用弧度。代码的工作原理如下: window.setInterval
我正在尝试平滑数字列表,出于绘图目的,我不想绘制超过 5000 个项目的图表,因为渲染时间太长,而且我们不需要额外的数据。 假设我们有一个简单的列表: let v = [1,2,3]; // max
我正在尝试制作一款倒置飞扬的小鸟游戏,但我面临一个大问题。 我尝试从上到下生成管道障碍物,但动画看起来很像文本编辑器,而且不流畅,所以我该如何平滑它?我尝试使用 requestanimationfra
我有一些数据由不均匀采样的 2D 空间位置组成,其中每个 x, y 坐标都有一个介于 0 和 2pi 之间的相关相位值 theta。我希望能够将 theta 值插入到常规 x, y 网格中。在相同(或
我有一个(3D)直方图,我喜欢对其应用高斯平滑: cv::MatND Hist; 在 1D 和 2D 情况下,我通过以下方式对其进行模糊处理: cv::GaussianBlur(Hist, Hist,
我的应用程序通过调整其 alpha 值在各种媒体和文本层之间淡入淡出。然而,当使用线性交叉淡入淡出时,亮度似乎在中途“下降”,然后又淡出。经过一番搜索,我找到了 this answer这解释了这个问题
我必须在屏幕上拖动一些 View 。我正在通过触摸监听器的 ACTION_MOVE 上的运动事件更改其布局参数的左侧和顶部来修改它们的位置。有没有办法让“拖动”元素更顺畅?因为这种“拖拽”一点也不顺畅
我只想问有没有你推荐的资源描述Image smoothing简单地。 谢谢。 最佳答案 就像一些评论者提到的那样,图像平滑可能意味着很多事情。但主要是,当有人使用这个术语时,他们的意思是模糊或低通滤波
我有一个带有简单视差的 React 组件,可以更改顶部和不透明度值。问题是滚动动画有点不稳定。有什么办法可以平滑过渡吗?我在 vanilla JS 中使用 requestAnimationFrame(
我在使用 libGDX 获得平滑字体时遇到问题。我已经在这个网站上搜索,并在谷歌上,我尝试了这些问题的解决方案here和 here ,但我的字体渲染效果总是很差。 示例: 我尝试了多种方法,总是得到与
我正在尝试为 Himmelblau's function 绘制一个简单的等高线图(在 gnuplot 中)使用以下代码: f(x,y)=(((x**2)+(y)-11)**2)+(((x)+(y**2
我想知道如何平滑 JScrollPane 的自动过渡。我正在使用 JScrollBar scroll = scollpane.getVerticalScrollBar(); scroll.setVal
我有一个高分辨率的healpix贴图(nside = 4096),我想在给定半径(假设为10 arcmin)的圆盘中对其进行平滑处理。 对healpy非常陌生,在阅读了文档后,我发现一种不太好的方法是
我使用下面的代码在 anchor 链接上平滑滚动 jQuery(function() { jQuery('a[href*=#]:not([href=#])').click(function() {
与这个问题非常相似:Rx IObservable buffering to smooth out bursts of events ,我有兴趣消除可能突然发生的可观察量。 希望下图说明我的目标: Ra
我正在开发代码战链接(一个 div)。我希望 div 内的 svg 在悬停时连续旋转。产生的悬停效果远非平滑,当鼠标在其上滚动时 svg 会跳跃。 mouseout 事件被注释掉了,因为它值得。优化这
有没有办法平滑转换(平移和旋转)的 BufferedImage 的锯齿状边缘? 测试图像的放大 View : (请注意,这不是将要使用的实际 BufferedImage,仅用于此处演示)。 已使用双线
为什么这没有产生平滑的圆?有什么想法吗? public void draw(ShapeRenderer sRenderer) { sRenderer.begin(ShapeType.Fille
我正在做一个项目,我的 ImageView 包含文本和曲线。当在计算机上查看图形时,它们看起来很好。如果带有 ImageView 的应用程序加载到 iPad 2 上,文本(字体:helvetica n
我是一名优秀的程序员,十分优秀!