0
$(window).scroll(function() {
  if ($(window).scrollTop() > 200) {
    $('#box').css({height:'100px', background-color:'green', overflow:'visible', width:'550px', margin:'0 auto'
  });

  $('#box2').css({position:'fixed',left:'400px',height:'200px',background: '#CF0'});
}

I am new to javascript, this might as well be a quick question...

What I have here is modifying the css styles of a div (#box) depending on the location of the page you are on (> 200 in this case).

What I am trying to achieve is to add multiple css alterations to #box and to execute a random one each time. For example each time you scroll pass 200 from the very top, the div appears either in height:'100px' with green background, height:'200px' with blue background, OR height:'300px' with red background. Hope this make sense....

4
  • The code you posted seems to be doing that. What's the problem you're having? Commented Nov 1, 2013 at 18:11
  • Yeah it works, but I want to add random css multiple alterations to one of the divs, and I have trouble looking for a solution to that, thank you Commented Nov 1, 2013 at 18:13
  • You have to be more specific... What do you want to be random? Did you try anything? Do you know how to generate a random number with js? Commented Nov 1, 2013 at 18:28
  • Sorry if the question was vague. So what I want is to execute either one of these 3 different css variables. Not a random number. 1. 100px in green background 2. 200px in yellow background 3. 300px in red background Commented Nov 1, 2013 at 18:37

1 Answer 1

1

You may try something like this (adjust your scrollTop value and css style)

JS:

var classes = ['a', 'b', 'c'];
$(window).scroll(function() {
    if ($(window).scrollTop() > 20) {
        var cls = classes[Math.floor( Math.random() * classes.length )];
        $('#box').toggleClass(cls);
    }
 });

CSS:

.a {
    background:red;
    height:100px;
}
.b {
    background:green;
    height:200px;
}
.c {
    background:blue;
    height:300px;
}

DEMO.

Update : toggles only once every scrolldowm

$(function(){
    var classes = ['a', 'b', 'c'];
    function down()
    {
        if ($(window).scrollTop() > 20 ) {
            var cls = classes[Math.floor( Math.random() * classes.length )];
            $('#box').toggleClass(cls);
            $(window).off('scroll.down');
        }
    }

    $(window).on('scroll.down', down).on('scroll.up', function(){
        if ($(window).scrollTop() < 20 ) {
            $(window).on('scroll.down', down);
        }
    });
});

DEMO.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Seems to be working...however it's constantly changing the class as you scroll. I meant to it stay at a particular class of the three.
Thanks! Seems to be working...however it's constantly changing the class as you scroll. I meant for it to stay at a particular class of the three. @RCV

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.