简体   繁体   English

当元素移出窗口左侧时的条件

[英]If condition for when an element moves off left-side of window

I am trying to detect when an element, that a jQuery plugin makes draggable, moves off the left edge of the window. 我正在尝试检测jQuery插件可拖动的元素何时移出窗口的左边缘。

I have a if condition in the on drag custom-event. 我在拖动自定义事件中有一个if条件。 The event fires on drag, but the alert inside the condition doesn't fire when the element moves off the left edge of the screen. 该事件在拖动时触发,但是当元素移出屏幕左边缘时,条件内的警报不会触发。

This leads me to think there is something wrong with my if-condition. 这使我认为我的if条件有问题。

To me, this reads going left and when anything greater than 0% is off the left edge of the screen. 对我来说,这是向左读取的内容,当大于0%的内容位于屏幕的左边缘时。

$('#tabViewWindow').pep({
    axis:'x',
    useCSSTranslation:false,
    drag:function(ev, obj){
    if(obj.dx>0 && obj.$el.offset().left>$(window).width()*0){
        obj.velocityQueue = new Array(5);
        alert("left and moving towards off screen");
        return false;
    }
    handleOpacity(ev, obj)
    },
 });
 function handleOpacity(ev, obj){
    var opacity = 1 - (parseInt(obj.$el.css('left'))/$(window).width());
    obj.$el.css('opacity', opacity);
 }

I'd recommend taking the elements' bounding box as it has been computed for the page the element is on by the browser, using element.getBoundingClientRect().left (this might be identical to jQuery's offset.left) and simply checking if it's less than 0. If so, your element is at least partially off screen. 我建议使用元素的边界框,因为它是为浏览器在其上的页面计算的,使用element.getBoundingClientRect().left (这可能与jQuery的offset.left相同),然后简单地检查它是否小于0。如果是,则您的元素至少部分不在屏幕上。 Then, as second check, if element.getBoundingClientRect().left + element.getBoundingClientRect().width is smaller than 0, your element is entirely off-screen. 然后,作为第二检查,如果element.getBoundingClientRect().left + element.getBoundingClientRect().width小于0,则您的元素完全在屏幕外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM