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

javascript - Toggle CSS with jQuery - Stack Overflow

programmeradmin7浏览0评论
$('.work').click(function(e){
    $('.information-overlay').fadeOut();
    $('.work-overlay').fadeToggle();
    $('body').toggleClass('overflow');
    e.preventDefault();

});

$('.information').click(function(e){
    $('.work-overlay').fadeOut();
    $('.information-overlay').fadeToggle();
    $('body').toggleClass('overflow');
    e.preventDefault();

});

I've got the following code that toggles an overlay, when .information is clicked and also toggles between overflow:hidden with toggleClass.

However, if I click .work and THEN .information, it breaks the toggleClass.

$('.work').click(function(e){
    $('.information-overlay').fadeOut();
    $('.work-overlay').fadeToggle();
    $('body').toggleClass('overflow');
    e.preventDefault();

});

$('.information').click(function(e){
    $('.work-overlay').fadeOut();
    $('.information-overlay').fadeToggle();
    $('body').toggleClass('overflow');
    e.preventDefault();

});

I've got the following code that toggles an overlay, when .information is clicked and also toggles between overflow:hidden with toggleClass.

However, if I click .work and THEN .information, it breaks the toggleClass.

Share Improve this question edited Aug 27, 2013 at 14:05 asked Aug 27, 2013 at 13:40 user1469270user1469270 1
  • 1 Create class with required elements and try toggleClass() – m1k1o Commented Aug 27, 2013 at 13:43
Add a ment  | 

6 Answers 6

Reset to default 5
$("body").css("overflow", "hidden");

JQUERY CSS

Or to toggle

CSS:

.hidden {
   overflow: hidden;
}

JQUERY:

$("body").toggleClass("hidden");   

You can use toggleClass for this.

use this

$("body").css("overflow", "hidden");

to add toggle functionality use http://api.jquery./toggleClass/

css

.b_overflow { overflow:hidden; }'

js

$('body').toggleClass('b_overflow');

updated after OP's ment

$('.work').click(function(e){
    $('.information-overlay').fadeOut();
    $('.work-overlay').fadeToggle();
    if($(this).css('display') =='block'){
        $(this).css('overflow','scroll');
    }else{
        $(this).css('overflow','hidden');
    }
    e.preventDefault();
});

jQuery code:

$("body").toggleClass("hidden");

And then create a class in your css:

.hidden {
  overflow: hidden;
}

this will world

#("body,html").css("overflow", "hidden");

the above solutions are perfect but toggling away body overflow bar will create a little lag on the website since page width will have difference.

Use $('body').toggleClass('overflow-hidden') to toggle a CSS class with the corresponding CSS styles

发布评论

评论列表(0)

  1. 暂无评论