Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement sizzle's :gt filter in vanilla javascript

I'm trying to port over a jQuery plugin I have to be compatible with AngularJS JQlite API but have come across a few road blocks. Here's the plugin:

    (function (e) {

    var $product = $('#product'),
        $imgs = $product.find('.child'),
        imageTotal = $imgs.length - 1,
        clicked = false,
        widthStep = 4,
        currPos,
        currImg = 0,
        lastImg = 0;
    $imgs.bind('mousedown', function (e) {
        e.preventDefault(); // prevent dragging images
    })
        .filter(':gt(0)').addClass('notseen');

    $product.bind('mousedown touchstart', function (e) {
        if (e.type == "touchstart") {
            currPos = window.event.touches[0].pageX;
        } else {
            currPos = e.pageX;
        }
        clicked = true;

    });
    $(document)
        .bind('mouseup touchend', function () {
        clicked = false;
    })
        .bind('mousemove touchmove', function (e) {
            fadeInOut();
        if (clicked) {
            var pageX;
            if (e.type == "touchmove") {
                pageX = window.event.targetTouches[0].pageX;
            } else {
                pageX = e.pageX;
            }
            widthStep = 4;
            if (Math.abs(currPos - pageX) >= widthStep) {
                if (currPos - pageX >= widthStep) {
                    currImg++;
                    if (currImg > imageTotal) {
                        currImg = 0;
                    }
                } else {
                    currImg--;
                    if (currImg < 0) {
                        currImg = imageTotal;
                    }
                }
                currPos = pageX;
                $imgs.eq(lastImg).addClass('notseen');
                $imgs.eq(currImg).removeClass('notseen');
                lastImg = currImg;
                // $obj.html('<img src="' + aImages[options.currImg] + '" />');
            }
        }
    });


}); 

All it's doing is creating 360 rotating effect, which can be demoed here.

Now the issue is some of these jquery specific APIs. Things like .filter(':gt(0)') to select items in an array with an index greater than 0 and pageX. I've done some research on .filter(':gt(0)') and came across a native javascript method of filtering but it seems to only filter a specific index (index of 2, 6, 13, not >0).

Any ideas on how to implement this filter in a pure javascript format? Any help is appreciated.

like image 903
mhartington Avatar asked Feb 10 '26 17:02

mhartington


1 Answers

Instead of using filter, you could use [].slice to get all indexes of the array greater than n.

$([].slice.call($imgs,1)).addClass("notseen");

http://jsfiddle.net/rm5PC/

this works in jquery too:

http://jsfiddle.net/rm5PC/1/

like image 175
Kevin B Avatar answered Feb 12 '26 14:02

Kevin B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!