简体   繁体   English

在jQuery .each循环中访问元素

[英]Accessing Elements in a jQuery .each loop

Good Morning, This is my second self-composed js; 早安,这是我第二本自编的js; apologies for the shallow depth of my knowledge and style. 对我的知识和风格的浅薄深表歉意。 I suspect my current solution is somewhat bubble-gum and duct-tape. 我怀疑我当前的解决方案是泡泡糖和胶带。

I've got a list of events that have a start time and end time. 我有一个具有开始时间和结束时间的事件列表。 They also have some assorted text data that cause the heights of the elements to vary. 它们还具有一些各种各样的文本数据,这些数据会导致元素的高度发生变化。 I'm using moment.js to convert times into appropriate widths and offsets. 我正在使用moment.js将时间转换为适当的宽度和偏移量。

I want to display the elements in as compressed a format as possible without overlapping. 我想以尽可能压缩的格式显示元素,而不会重叠。 So far I've got it working for most sets of events by storing the corner variables and the comparing them in the next iteration of the loop; 到目前为止,我通过存储拐角变量并在循环的下一个迭代中进行比较来使其适用于大多数事件。 and then moving the current element down to the bottom of the previous element. 然后将当前元素向下移动到上一个元素的底部。

Is there a better way to do this that allows me to compare an event to all the previous events and position it accordingly? 有没有更好的方法可以让我将一个事件与之前的所有事件进行比较并据此进行定位?

                    $('.metro_event').each(function (i) {

        // Set Width and Left Offset based on start time and duration
        pixelScale = 1.25
        startTime = $(this).attr('start_date');
        startTime = moment(startTime);
        endTime = moment($(this).attr('end_date'));
        endTime = moment(endTime);
        duration = moment.duration(endTime - startTime);
        dayStart = moment(startTime).startOf('day');
        timeOffsex = moment.duration(startTime - dayStart);
        timeOffset = ((timeOffsex - 32400000)/60000) * 1.25;
        timeWidth = (duration/60000) * 1.25;
        $(this).css("width", timeWidth + "px");
        $(this).css("left", timeOffset + "px");

        //Get Height of Current for comparison
        currentHeight = $(this).height();
        currentBottom = currentHeight

        // Get Paramters for Previous Positioned Element

        lastWidth = $(".positioned").filter(':last').width();
        lastHeight = $(".positioned").filter(':last').height();
        lastLeft = $(".positioned").filter(':last').css("left");
        lastLeft = lastLeft.substring(0, lastLeft.length - 2);
        lastLeft = lastLeft * 1
        lastTop = $(".positioned").filter(':last').css("top");
        lastTop = lastTop.substring(0, lastTop.length - 2);
        lastTop = lastTop * 1
        lastRight = lastLeft + lastWidth;
        lastBottom = lastTop + lastHeight;

        // Compare Placement to Previous Positioned Element and Move Down as required

        if (timeOffset<lastRight && currentHeight>lastTop)
            {
            $(this).css("top", lastBottom + "px");
        }

        $(this).addClass('positioned');

        // Increment Height and Set Height of Containing Div

        $('#events').css("height", "300px");


    });

Don't use $(".positioned").filter(':last') (and don't use if over and over, but only once). 不要使用$(".positioned").filter(':last') (并且一遍又一遍不要使用,只能使用一次)。 I think what you really want is a variable containing the last iterated element, isn't it? 我想你真正想要的是包含上次迭代元素的变量,不是吗?

var last = null; // or $(".positioned").filter(':last') if there are already some
$('.metro_event').each( function() {
    var $this = $(this);
    // get attributes,
    // compute moments,
    // get css styles and position values

    // Now, use the variable
    last …
    // to get position values etc of the last element
    // style $this and
    $this.addClass("positioned");

    // then, after you're done, set "last" for the next iteration
    last = $this;
});
// maybe clean the global? variable last again with
last = null;

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

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