Sqnkov Sqnkov - 1 year ago 83
Javascript Question

Toggle .dimBackground() method on click

I am trying to dim the background of my main content while accenting on a sidebar that needs to show up from the right, so I am using the dim-background jquery plugin. I want to toggle the

method for my element when I click its trigger. It adds a div with the class of
and it simply stacks this div when I click the trigger, making everything darker and darker.

I want to be able to toggle both the sidebar and the dimming above the main content. And if possible, to be able to toggle it if I click outside the sidebar as well.

Sample code from the fiddle:

$('.trigger').click(function() {
darkness: 0.35

There is no CDN that provides this library, however, here is the Fiddle describing my problem.

Answer Source

According to the Usage guide, there's a .undim method. It seems that .dimBackground does not toggle, so you will have to store the state on your end and invoke either .dimBackground or .undim depending on the state and switch it.


var dimmed = false;
$('.trigger').click(function() {
  var $sidebox = $('.sidebox'); //avoid searching the element twice
      dimmed = false;
  } else {
        darkness: 0.35
      dimmed = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download