1

I have tried several ways to create a parallax effect on my images where each image has it's own random speed. I think the best way for me to achieve it is by assigning a speed value to each image. However I am not sure how to do this.

var img_list = [];
$.each($('.gallery_image'), function(e) {
    img_list.append($(this));
});

for(x in img_list) {
    ran = Math.round(Math.random() * (11 - 1) + 1);
    speed = ran.toString() + "px";
    x.speed = speed;
}

This is what I came up with. I know that x.speed is not an actual thing, but I am using that to illustrate what I am trying to accomplish.

This is a website that has exactly what I am looking for on the main page, but I want the movement to be on scroll. EXAMPLE

5
  • That for..in won't work. Commented Dec 15, 2016 at 6:41
  • I know, but I'm not sure how to assign a speed value to the jquery objects Commented Dec 15, 2016 at 6:42
  • Why don't you use $.each to loop the img_list ? Commented Dec 15, 2016 at 6:46
  • 1
    x is a key (1,2,3...) so you need img_list[x].speed Commented Dec 15, 2016 at 6:48
  • Could you print.the content of img_list? Didnyou try to iterate with for (i=0; i<img_list.length; i++)?? Commented Dec 15, 2016 at 6:55

3 Answers 3

2
for(var x in img_list) {
    var ran = Math.round(Math.random() * (11 - 1) + 1);
    img_list[x].speed = ran.toString() + "px";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry been coding for about 8 hours straight and its 2am. Thank you that works. Do you have a suggestion on how to make the parallax? Just with a .css changing the position on scroll?
1

Use push

var img_list = [];
$.each($('.gallery_image'), function(e) {
    img_list.push($(this));
});

but in this case you don't need a loop because $('.gallery_image') is a collection of objects

In the second loop you can use a each loop and save the speed as a data attribute:

$('.gallery_image').each(function(i,x){
    ran = Math.round(Math.random() * (11 - 1) + 1);
    speed = ran.toString() + "px";
    $(x).data('speed',speed);
});

2 Comments

"$('.gallery_image') is a array of objects" - Well, it's a jQuery object, which is array-like (but not an actual array). Might be better to use .data() rather than .attr().
@nnnnnn thnx for the feedback
1

I dont know if this is relative or not ,but I made a demo . Hope this helps.

To move image set left css property of dom.

DEMO

https://jsfiddle.net/vikrant47/yem6f0Ls/4/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.