jQuery:获取高于给定"y"位置的元素 [英] jQuery: get elements above a given 'y' position

查看:123
本文介绍了jQuery:获取高于给定"y"位置的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何用一种优雅的方式使用jQuery?

How can you do this with jQuery, in an elegant way?

z属性(例如:红色背景)应用于div parent
的每个子项 而它们的位置在给定的最高偏移y之上.

Apply z attribute (e.g.: red background) to every children of a div parent
while their position is above a given top-offset y.

我尝试了不同的方式,但是我对其中的任何一个都不满意...
我知道一定有一种简短而优雅的方法来做...

I've tried in different ways, but I'm not happy with any of them...
I know there must be a short and elegant way to do it...

推荐答案

由于您是说您已经尝试了几种方法,而您只是在寻找更优雅的东西,所以我假设您有偏移部分算了,我自己就和offset一起去.根据需要修改该部分.为了优雅起见,您可以创建一个自定义选择器来检查顶部偏移量:

Since you're saying you've tried a few ways, and you're just looking for something more elegant, I'll assume you have the offset part worked out, and I'll just go with offset myself. Modify that part as needed. For elegance, you could create a custom selector checking top offset:

$.expr[':'].above = function(obj, index, meta, stack) { 
    return $(obj).offset().top < meta[3];
}

然后您可以这样查询它:

You could then query it as such:

$('#myParentDiv').find('div:above(100)').css('background-color', 'red');

当然,这也可以表达为

$('#myParentDiv div:above(100)').css('background-color', 'red');

或者,如评论中指出的

var y = 100;
$('#myParentDiv div:above('+y+')').css('background-color', 'red');

这篇关于jQuery:获取高于给定"y"位置的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆