Tom Foster
Tom Foster

Reputation: 462

Call jQuery function except specific pages

I'm new to jQuery and I'm having trouble with the .not() function.

I'm trying to call a function except for certain pages, with the code below, specifically the bottom line, with no success:

$(window).load(function()
{
    function fixDiv() 
    {
        var $div = $('td#cke_top_edit-body-und-0-value');      

        if ($(window).scrollTop() > 245) 
        {
            $div.css({'background':'grey', 'position': 'fixed', 'top': '0', 'width': '100%'}); 
        }
        else 
        {
            $div.css({'background':'none','position': 'static', 'top': 'auto', 'width': '100%'});
        }
    }

    $(window).not('body.page-node-add-event, body.page-node-add-activity, body.page-node-add-board-agenda, body.page-node-add-daily-lunch-menu, body.page-node-add-event, body.page-node-add-job-posting').scroll(fixDiv);});

I appreciate any suggestions! It's still calling the function on all pages.

Upvotes: 1

Views: 147

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

$(function(){
    function fixDiv() {
        var $div = $('td#cke_top_edit-body-und-0-value');      

        if ($(window).scrollTop() > 245) {
            $div.css({'background':'grey', 'position': 'fixed', 'top': '0', 'width': '100%'}); 
        } else {
            $div.css({'background':'none','position': 'static', 'top': 'auto', 'width': '100%'});
        }
    }

    if(!$('body').is('.page-node-add-event, .page-node-add-activity, .page-node-add-board-agenda, .page-node-add-daily-lunch-menu, .page-node-add-event, .page-node-add-job-posting')) {
        $(window).scroll(fixDiv);
    }
});

But instead of adding the class for each page like above, I would suggest you to add a class like no-scroll to those pages in which you do not want to have the scroll enabled then

    if(!$('body').is('.no-scroll')) {
        $(window).scroll(fixDiv);
    }

Upvotes: 1

Related Questions