0

I'm working on a popup menu for mobile devices and would like for the website to blur and lighten in opacity when the menu pops up and go back to normal when it's closed. I figured a good way to go about doing it would be by triggering a css filter which led me to be unsure of the proper syntax to use in JQUERY. I looked into the matter more and so far I haven't been able to find examples of css filters being triggered in jquery so I continued playing with it to see if I could get it to work and so far have been unsuccessful.

Here are the scripts I came up with.

$("#menu").click(function(){
    $("#popup").fadeIn('slow');
    $("#close").css("display", "block");
    $("#menu").css("display", "none");
    $("p").css("opacity", 0.33);
    );

$("#close").click(function(){
    $("#popup").fadeOut('slow');
    $("#close").css("display", "none");
    $("#menu").css("display", "block");
    $("p").css("opacity", 1);
    });

The way I was trying to add in the css filter is

//This one shows no sign of the blur working but opacity works
$("p").css({"opacity": "0.33",
            "filter": "blur(88%)",
            "-webkit-filter": "blur(88%)",
            "-moz-filter": "blur(88%)"
             });



//These two break the whole code from working at all
$("p").css({"opacity": "0.33",
            "filter: blur(88%)",
            "-webkit-filter: blur(88%)",
            "-moz-filter: blur(88%)"
            });

$("p").css({"opacity": "0.33",
            "filter: blur()" "88%",
            "-webkit-filter: blur()" "88%",
            "-moz-filter: blur()" "88%" 
             });

Of course these are attempts to create the blur which I added to the "menu" button. Here's a fiddle of it https://jsfiddle.net/Optiq/hsvvpfzu/5/

The more I looked and couldn't find anything made me wonder if this is at all possible. Maybe it's just me not knowing specific enough stuff to search for in order to find something relevant. Any source of info talking about using css filters in jquery are welcome.

UPDATE

The fiddle just simulates the code I've been working with on my site so it wasn't clear as to the element I was trying to effect. I have each page wrapped in a div that has a class name but no id. The reason I didn't give it an id is because I use it over and over on each page, so I figured it would be cleaner to just use the jquery to target the class and add attributes that way rather than giving each div a unique id then passing them all into a variable or something.

7
  • 2
    Have you tried just preparing classes for the css rules and then switching the element's classes with addClass and removeClass? Commented Sep 17, 2016 at 5:29
  • 1
    What does limit you from using CSS classes? Commented Sep 17, 2016 at 5:30
  • 1
    i am agree with using class @Don Bhrayan Singh Commented Sep 17, 2016 at 5:37
  • I just updated the post to show why I'm trying to do it this way. Thanks for pointing that out guys. Commented Sep 17, 2016 at 5:38
  • 2
    According to MDN, blur() does not accept percentage values. Have you tried changing them to pixel values? Commented Sep 17, 2016 at 5:42

2 Answers 2

2

Of your three examples, the first one uses the correct syntax. The problem is that blur doesn't accept percentage values, only pixels. Defining them as pixels as such appears to have the desired effect for me:

$("p").css({
    "opacity": "0.33",
    "filter": "blur(1px)",
    "-webkit-filter": "blur(1px)",
    "-moz-filter": "blur(1px)"
})

Hope this helps! :)

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

Comments

2

Why not you assign and remove clss instead of css, implement with class is better one, you can also assign CSS but it require more code.

$(document).ready(function(){
$("p").addClass("myClass");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<style>
  .myClass{
    opacity: 0.33;
    filter: blur(88%);
    -webkit-filter:blur(88%);
     -moz-filter: blur(88%);
    color:red;
  }
 </style>
 
<p>Sandip Patel</p>

2 Comments

Sorry I just updated the post to address this. The reason why is because it's a div that I use as a container for the entire page and I use it over on each of my pages. So I didn't give it an id because so far I didn't think I'd need to trigger it with an id. So my first mind was to just call up the class with jquery and modify the css attributes, instead of having to pass a bunch of different IDs through it.
There are 2 different issues. targeting where to apply the changes (using whatever selectors you want) is indepenent of what you done once you have get the elements . I believe adding a class with the blur style is your best option.

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.