最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If-statement with div margin-top as condition? - Stack Overflow

programmeradmin2浏览0评论

I'd like to create an if - else condition system which allows scrolling up only when the top margin is different than 0. I'll use the .animate action in jquery to create an animation.

$('#up-arrow a').click(function(){
    if('#slider'.css(margin-top)!='0px')
        $('#slider').animate({'margin-top':'+=320px'});
    else

Can someone please correct this for me? It works without the if, but after I added the if-else it does nothing.

I'd like to create an if - else condition system which allows scrolling up only when the top margin is different than 0. I'll use the .animate action in jquery to create an animation.

$('#up-arrow a').click(function(){
    if('#slider'.css(margin-top)!='0px')
        $('#slider').animate({'margin-top':'+=320px'});
    else

Can someone please correct this for me? It works without the if, but after I added the if-else it does nothing.

Share Improve this question asked Nov 28, 2011 at 19:10 Alex G.Alex G. 2772 gold badges5 silver badges14 bronze badges 1
  • 1 Hint: if('#slider'.css(margin-top)!='0px') – jsalonen Commented Nov 28, 2011 at 19:12
Add a ment  | 

2 Answers 2

Reset to default 5

Instead of this

if('#slider'.css(margin-top)!='0px')

Try this

if($('#slider').css("margin-top")!='0px')

If else statements require curly braces. If statements with a single expression may be written without curly braces but this practice is usually advised against.

Try this:

$('#up-arrow a').click(function(){
    if($('#slider').css("margin-top")!='0px') {
        $('#slider').animate({'margin-top':'+=320px'});
    } else {
        //whatever you want to do here...
    }
});
发布评论

评论列表(0)

  1. 暂无评论