我正在从事一个项目,该项目使用箭头键作为焦点处理的一种形式,并且在我的列表滚动中遇到一些严重的卡顿。我重新创建了一个 -- JSFiddle -- 展示正在发生的事情,但它在 fiddle 中看起来更好。我认为这是因为我使用 scrollTop 重新绘制的元素对于我的应用程序来说要复杂得多。在不使用 scrollTop 的情况下,有没有更好的方法来做到这一点?我知道它会导致重新布局,并且很好奇是否有更好的方法。
这是来自 -- JSFiddle 的主要代码--
function scroll() {
var focusedBox = focused.getBoundingClientRect();
if (focusedBox.bottom > containerBox.bottom || focusedBox.top < containerBox.top) {
requestAnimationFrame(function() {
var distance = focusedBox.height + 10;
animate(distance, focusedBox.top < containerBox.top);
});
}
}
function animate(distance, up) {
if (distance >= speed) {
container.scrollTop += (up ? -speed : speed);
requestAnimationFrame(function() {
animate(distance - speed, up);
});
} else {
container.scrollTop += (up ? -distance : distance);
}
}
** 要尝试一下,请确保在 fiddle 输出区域内单击以便触发按键事件,然后使用向下/向上箭头 **
我还需要滚动条才能工作,所以如果唯一更好的选择是使用 CSS3 transformY,我将不得不构建一个自定义滚动条。
最佳答案
在没有看到实际代码的情况下很难说,但请查看这个 fiddle ,看看它是否对任何事情有帮助:
http://jsfiddle.net/fxyuzo6z/4/
基本上,我已经删除了您在每个焦点事件上运行的脉冲动画,以进行偏移,直到按键之间出现明显的延迟。这为浏览器需要渲染的其他动画提供了更多资源,希望能消除您注意到的卡顿/卡顿现象。可以根据您的需要调整超时延迟
CSS:
.focused {
-webkit-box-shadow: inset 0px 0px 0px 3px rgba(255,255,255,1);
-moz-box-shadow: inset 0px 0px 0px 3px rgba(255,255,255,1);
box-shadow: inset 0px 0px 0px 3px rgba(255,255,255,1);
}
.focused.animate {
-webkit-animation: pulse 1.8s infinite ease-in-out;
-moz-animation: pulse 1.8s infinite ease-in-out;
animation: pulse 1.8s infinite ease-in-out;
}
JS:
var pool = document.querySelectorAll('.item-row')
, container = document.getElementById('item-container')
, containerBox = container.getBoundingClientRect()
, focused = pool[0]
, focusIndex = 0
, KEYS = {up: 38, down: 40}
, keypressTimer = null;
window.addEventListener('keyup', function(e) {
if (e.keyCode === KEYS.up && focusIndex !== 0) {
focusIndex--;
setFocus()
} else if (e.keyCode === KEYS.down && focusIndex !== pool.length - 1) {
focusIndex++;
setFocus()
}
});
function setFocus() {
clearTimeout(focused);
focused.classList.remove('animate');
focused.classList.remove('focused');
focused = pool[focusIndex];
focused.classList.add('focused');
scroll();
keypressTimer = setTimeout(function() {
focused.classList.add('animate');
}, 1000);
}
function scroll() {
var focusedBox = focused.getBoundingClientRect();
if (focusedBox.bottom > containerBox.bottom || focusedBox.top < containerBox.top) {
requestAnimationFrame(function() {
var distance = focusedBox.height + 12;
animate(distance, focusedBox.top < containerBox.top, 20);
});
}
}
function animate(distance, up, speed) {
if (distance >= speed) {
container.scrollTop += (up ? -speed : speed);
requestAnimationFrame(function() {
animate(distance - speed, up, speed);
});
} else {
container.scrollTop += (up ? -distance : distance);
}
}
希望这对您有所帮助!
编辑:
我又进行了一项(未进行基准测试)超优化(希望如此)测试,通过将您在每次按键时执行的次要计算卸载到 Web Worker 中来提高性能。显然这不是跨浏览器的解决方案,因此是否值得尝试由您决定:
关于javascript - 动画 scrollTop 在固定高度溢出时没有卡顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569692/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u
我在非Rails项目中使用ActiveRecord。在Rails中,我可以这样做:config.time_zone='EasternTime(US&Canada)'config.active_record.default_timezone='EasternTime(US&Canada)'但如果我不使用rails,我该如何设置时区? 最佳答案 ActiveRecord::Base.default_timezone='EasternTime(US&Canada)' 关于ruby-没有轨道的A
在我让另一个人重做我的前端UI之前,我的Rails应用程序运行平稳。我已经尝试解决此错误3天了。这是错误:Nosuchfileordirectory-identifyExtractedsource(aroundline#59):575859606162@post=Post.find(params[:id])authorize@postif@post.update_attributes(post_params)flash[:notice]="Postwasupdated."redirect_to[@topic,@post]else{"utf8"=>"✓","_method"=>"patc