Chun
Chun

Reputation: 2270

Toggle slider button image go back to normal

I have 2 images as a button which changes whether a person clicks to open or close the slider and I also have the stopPropagation event which closes the slider if I click anywhere outside the div(slider)

What I want now is to make the image go back to normal(to the first image displayed as a button) if a person clicks outside the div, the same way if they where clicking the button to close again, because if I open the slider by clicking the button and then click outside the div to close it the image doesn't change. Here's my code: http://jsfiddle.net/v0yfb72v/

jQquery:

$('#toggle-onoff-network').on({
    'click': function () {
        var origsrc = $(this).attr('src');
        var src = '';
        if (origsrc == 'http://www.pulseframe.com/images/Button-on.png') src = 'http://www.pulseframe.com/images/Button-off.png';
    if (origsrc == 'http://www.pulseframe.com/images/Button-off.png') src = 'http://www.pulseframe.com/images/Button-on.png';
    jQuery(this).attr('src', src);
    }
});

// The script
$(document).ready(function(){
    $('#netywork-toggle').click(function(event){
        event.stopPropagation();
         $("#voyaflex-container").slideToggle("fast");
    });
    $("#voyaflex-container").on("click", function (event) {
        event.stopPropagation();
    });
});

$(document).on("click", function () {
    $("#voyaflex-container").hide();
});

Upvotes: 0

Views: 256

Answers (3)

Dave Zych
Dave Zych

Reputation: 21887

Update your document click event to fire the click event of the button. It will toggle the button and close the slider for you:

$(document).on("click", function () {
    if($("#voyaflex-container").css('display') !== 'none')
    {
        $('#toggle-onoff-network').click();
    }
});

Fiddle: http://jsfiddle.net/6xezqh7e/

Upvotes: 1

Mathieu Labrie Parent
Mathieu Labrie Parent

Reputation: 2606

Simply wrap your click in a function.

function ChangeImage() {
    var origsrc = $('#toggle-onoff-network').attr("src");
    var src = '';
     if (origsrc == 'http://www.pulseframe.com/images/Button-on.png') src = 'http://www.pulseframe.com/images/Button-off.png';
    if (origsrc == 'http://www.pulseframe.com/images/Button-off.png') src = 'http://www.pulseframe.com/images/Button-on.png';
    jQuery(this).attr('src', src);

}

$('#toggle-onoff-network').on({
  'click': function() {
      ChangeImage();
 }
});

$(document).on("click", function () {
    $("#voyaflex-container").hide();
    ChangeImage();
});

Upvotes: 0

user2019037
user2019037

Reputation: 762

You can add same action on document click also instead of hide toggle the section like:

// Change toggle when clicked
$('#toggle-onoff-network').on({
  'click': function() {
    var origsrc = $(this).attr('src');
    var src = '';
    if (origsrc == 'http://www.pulseframe.com/images/Button-on.png') src = 'http://www.pulseframe.com/images/Button-off.png';
    if (origsrc == 'http://www.pulseframe.com/images/Button-off.png') src = 'http://www.pulseframe.com/images/Button-on.png';
    jQuery(this).attr('src', src);
  }
});


// The script
$(document).ready(function() {
  $('#netywork-toggle').click(function(event) {
    event.stopPropagation();
    $("#voyaflex-container").slideToggle("fast");
  });
  $("#voyaflex-container").on("click", function(event) {
    event.stopPropagation();
  });
});

$(document).on("click", function() {
  $("#voyaflex-container").toggle();
  
  //add this part of code
  var origsrc = $('#toggle-onoff-network').attr('src');
  var src = '';

  if (origsrc == 'http://www.pulseframe.com/images/Button-on.png') src = 'http://www.pulseframe.com/images/Button-off.png';
  if (origsrc == 'http://www.pulseframe.com/images/Button-off.png') src = 'http://www.pulseframe.com/images/Button-on.png';
  $('#toggle-onoff-network').attr('src', src);

});
#netywork-toggle {
  background: #000;
  padding: 5px;
}
#voyaflex-container {
  background: #E5980C;
}
#netywork-toggle img {
  max-height: 25px;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<style type="text/css">
  #voyaflex-container {
    display: none
  }
</style>
<div id="netywork-toggle">
  <img id="toggle-onoff-network" src="http://www.pulseframe.com/images/Button-on.png" width="auto" height="34" />
</div>
<div id="voyaflex-container">
  <!-- social share buttons -->
  <ul class="crafty-social-buttons-list">
    <li>
      <a href="#" target="_blank">
        <img title="Pulseframe Code" alt="Facebook" width="38" height="38" src="facebook.png">
      </a>
    </li>
    <li>
      <a href="#" target="_blank">
        <img title="Pulseframe Geek" alt="twitter" width="38" height="38" src="facebook.png">
      </a>
    </li>
    <li>
      <a href="#" target="_blank">
        <img title="Comming Soon" alt="Comming Soon" width="38" height="38" src="facebook.png">
      </a>
    </li>
    <li>
      <a href="#" target="_blank">
        <img title="Comming Soon" alt="Comming Soon" width="38" height="38" src="facebook.png">
      </a>
    </li>
  </ul>
</div>

Upvotes: 1

Related Questions