简体   繁体   中英

formulation of condition in if-statement

I have this:

$(function() {
    if () {
        $('#content').css('position', 'fixed');
    });
});

So, now what I want: If the 'top' of '#content' is 0, I want the css that is written down in the function to happen. But I don't know how to write that down in between the brackets after 'if'.

Maybe someone can help me with this basic if-statement?

Much thanks in advance!

edit-----------------------------

Seems to work better and better, still one problem though.. I now have this:

$(function() {
    if ( $('#content').offset().top == 0) {
        $('#content').css({'position' : 'fixed'});
    }
    else {
        $('#content').css({'position' : 'relative'});
    }
});

And this:

$(document).ready(function() {
    $('#content').scrollTop('100%');
});

But now the div is immediately 'fixed'..

FIDDLE

Use $('selector').offset().top to get the numeric value of the top position.

if ($('#content').offset().top == 0) {
    $('#content').css('position', 'fixed');
});

for more information you can see this

Also set your css in condition like this

$('#content').css({'position' : 'fixed'});

In case of your scroll to top you can do like this

$('#content').scrollTop(yourtopvalue); // your top value goes here.

For animated effects, you could do like this also

$('selector').animate({scrollTop:$('#content').offset().top}, 'slow');

You can mix up these stuffs like these ways using jQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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