I have a simple request but I have a a toggle button which basically hides a div off screen to the left which works fine. The button is on the inside of the div so when it hides the div you are unable to open it again because the button is off the screen. Now I have created a new button to open it but I only want it to show once the div has been hidden, so its like a tab just showing to open the div again.
// Slider info box toggle
function toggleDivs() {
var $inner = $("#slideToggle");
if ($inner.position().left == 0) {
$inner.animate({
left: "-840px"
});
}
else {
$inner.animate({
left: "0px"
});
}
}
$(".sliderClose").bind("click", function() {
toggleDivs();
});
$(".sliderOpen").bind("click", function() {
toggleDivs();
});
Assuming the button you want to hide/show has class .slideOpen then
function toggleDivs() {
var $inner = $("#slideToggle");
if ($inner.position().left == 0) {
$inner.animate({
left: "-840px"
});
$(".sliderOpen").fadeIn();
}
else {
$inner.animate({
left: "0px"
});
$(".sliderOpen").fadeOut();
}
}
$(".sliderClose").bind("click", function() {
toggleDivs();
});
$(".sliderOpen").bind("click", function() {
toggleDivs();
});
Related
I am working on closing toggle menu for mobiles and having a small problem. So what i want is when the toggle menu is active, user to be able to close it by touching somewhere on the screen on his device. I almost got it working, but when closed the basket in the header disappears and the menu doesn't retrieve to a hamburger icon. I am working on Wordpress website, just to notice.
I guess the problem comes from this: aria-expanded="true" , because the default value should be false after the user has closed it.
So my website is:
https://www.ngraveme.com/bg
my JQuery code is:
jQuery(document).ready(function($) {
var $menu = $('.menu');
$('.menu-toggle').click(function() {
$menu.toggle();
});
$(document).mouseup(function(e) {
if (!$menu.is(e.target) // if the target of the click isn't the container...
&&
$menu.has(e.target).length === 0) // ... nor a descendant of the container
{
$menu.hide();
}
});
});
and the original js code written from the theme i am using in wordpress is:
/**
* navigation.js
*
* Handles toggling the navigation menu for small screens.
* Also adds a focus class to parent li's for accessibility.
* Finally adds a class required to reveal the search in the handheld footer bar.
*/
(function() {
// Wait for DOM to be ready.
document.addEventListener('DOMContentLoaded', function() {
var container = document.getElementById('site-navigation');
if (!container) {
return;
}
var button = container.querySelector('button');
if (!button) {
return;
}
var menu = container.querySelector('ul');
// Hide menu toggle button if menu is empty and return early.
if (!menu) {
button.style.display = 'none';
return;
}
button.setAttribute('aria-expanded', 'false');
menu.setAttribute('aria-expanded', 'false');
menu.classList.add('nav-menu');
button.addEventListener('click', function() {
container.classList.toggle('toggled');
var expanded = container.classList.contains('toggled') ? 'true' : 'false';
button.setAttribute('aria-expanded', expanded);
menu.setAttribute('aria-expanded', expanded);
});
// Add class to footer search when clicked.
document.querySelectorAll('.storefront-handheld-footer-bar .search > a').forEach(function(anchor) {
anchor.addEventListener('click', function(event) {
anchor.parentElement.classList.toggle('active');
event.preventDefault();
});
});
// Add focus class to parents of sub-menu anchors.
document.querySelectorAll('.site-header .menu-item > a, .site-header .page_item > a, .site-header-cart a').forEach(function(anchor) {
var li = anchor.parentNode;
anchor.addEventListener('focus', function() {
li.classList.add('focus');
});
anchor.addEventListener('blur', function() {
li.classList.remove('focus');
});
});
// Add an identifying class to dropdowns when on a touch device
// This is required to switch the dropdown hiding method from a negative `left` value to `display: none`.
if (('ontouchstart' in window || navigator.maxTouchPoints) && window.innerWidth > 767) {
document.querySelectorAll('.site-header ul ul, .site-header-cart .widget_shopping_cart').forEach(function(element) {
element.classList.add('sub-menu--is-touch-device');
});
}
});
})();
Try replacing your jQuery code with this:
jQuery(document).ready(function($) {
$(document).mouseup(function(e) {
var $menuContainer = $('.menu');
var $menu = $menu.find('ul');
var $container = $('.site-navigation');
var $button = $container.find('button')
if (!$menuContainer.is(e.target) && $menuContainer.has(e.target).length === 0) {
if ($container.hasClass('toggled')) {
$button.attr('aria-expanded', false);
$menu.attr('aria-expanded', false);
}
}
});
});
It uses the vanilla-js code from the template for hiding/showing the menu, but with jQuery synthax.
I have a side menu that is working good. I need help with when menu is opened user can close the menu by click outside it.
$(document).ready(function()
{
$('#service-icon-button').click(function() {
if($(this).css("margin-left") == "480px")
{
$('.service-menu').animate({"margin-left": '-=480'});
$('#service-icon-button').animate({"margin-left": '-=480'});
}
else
{
$('.service-menu').animate({"margin-left": '+=480'});
$('#service-icon-button').animate({"margin-left": '+=480'});
}
});
});
below the jsfiddle sample
https://jsfiddle.net/ahmedcom/1j0wr4pL/8/
You can use this code for close a side menu by clicking outside of it
$(document).click(function (e) {
var container = $("#service-icon-button");
if (e.target.id == container[0].id) // if the target of the click isn't the TOGGLE BUTTON...
{
if ($("#service-icon-button").css("margin-left") == "480px") {
$('.service-menu').animate({ "margin-left": '-=480' });
$('#service-icon-button').animate({ "margin-left": '-=480' });
}
else {
$('.service-menu').animate({ "margin-left": '+=480' });
$('#service-icon-button').animate({ "margin-left": '+=480' });
}
}
else {
if ($("#service-icon-button").css("margin-left") == "480px") {
$('.service-menu').animate({ "margin-left": '-=480' });
$('#service-icon-button').animate({ "margin-left": '-=480' });
}
}
});
You can add a listener and a condition to check if the menu is opened:
$(document).on("click" , function () {
if($('#service-icon-button').css("margin-left") === "480px")
{
$('.service-menu').animate({"margin-left": '-=480'});
$('#service-icon-button').animate({"margin-left": '-=480'});
}
});
I have the following code:
var toggleButton = $('.menu-toggle'),
timerDelay;
toggleButton.on("click", function() {
var elem = $(this),
menu = elem.siblings('ul'),
parent = elem.parent(),
parentParent = parent.parent(),
pageH = $(window).height();
elem.toggleClass('pressed');
menu.stop(true);
clearTimeout(timerDelay);
if (menu.is('.scrollable')) {
menu.css({"max-height": pageH - 80});
}
parent.toggleClass('showed');
if (menu.is('.parent')) {
parentParent.toggleClass('showed');
}
if (menu.is('.hidden')) {
menu.css({"height": "100%", "padding": 5}).toggleClass('hidden showed');
} else {
menu.toggleClass('hidden showed');
if (menu.is('.nodelay')) {
menu.css({"height": 0, "padding": ""});
} else {
timerDelay = setTimeout(function() {
menu.css({"height": 0, "padding": ""});
}, 450);
}
}
});
This is the code for a pop-up menu. The problem is that it requires clicking on a specific button to close it. I'm trying to also make it close whenever the user clicks anywhere on the page.
Is there a way?
Maybe I gave the wrong code. This is another section:
$(document).click(function (e)
{
var container = $("#wrapper");
if (!container.is(e.target) && container.has(e.target).length === 0 && event.target.id!=="menu-toggle")
{
container.addClass("toggled");
}
});
Could I mix it with this?
$(document).mouseup(function (e)
{
var container = $("YOUR CONTAINER SELECTOR");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
$(document).one("click",function() {
if ($(".showed").is(":visible")) {
$(".pressed").trigger("click");
}
});
This would help, provided your menu has a class 'menu'.
$('.menu, .menu-toggle').on('click', function(e) {
e.stopPropagation();
});
$('body').on('click', function(e) {
if($('.menu').hasClass('showed')){
//code to hide menu
// i have clicked the toggle button here since the logic to hide menu is not separate in your code
toggleButton.click();
}
});
Here is an example jsfiddle:
https://jsfiddle.net/okbpxb14/
Without a working example...
You can do something like this:
var toggleButton = $('.menu-toggle'),
timerDelay;
toggleButton.on("click", toggleDisplay);
function toggleDisplay() {
var elem = $(this),
menu = elem.siblings('ul'),
parent = elem.parent(),
parentParent = parent.parent(),
pageH = $(window).height();
elem.toggleClass('pressed');
menu.stop(true);
clearTimeout(timerDelay);
if (menu.is('.scrollable')) {
menu.css({"max-height": pageH - 80});
}
parent.toggleClass('showed');
if (menu.is('.parent')) {
parentParent.toggleClass('showed');
}
if (menu.is('.hidden')) {
menu.css({"height": "100%", "padding": 5}).toggleClass('hidden showed');
$(window).addEventListener('click', toggleButton);
} else {
menu.toggleClass('hidden showed');
if (menu.is('.nodelay')) {
menu.css({"height": 0, "padding": ""});
} else {
timerDelay = setTimeout(function() {
menu.css({"height": 0, "padding": ""});
}, 450);
}
$(window).removeEventListener('click', toggleDisplay);
}
}
addEventListener to the window when it is show. When window is clicked run the toggle function.
removeEventListener from the window when it is hidden.
One issue this still has is that if the menu is clicked it will still hide, so you would need to add logic that if the menu is clicked to return;
Update After looking at you're live example.... Instead of using window you can use '.blog-content'
Couldn't find a solution that actually worked, but I want that on a click, a div shows.
Now this works when I load the page, but then after that first click, I have to click twice every time for the div to show.
Any ideas?
$(document).ready(function () {
setMenu();
});
function setMenu()
{
var headerExtIsOpen = false;
$('#headerExt').hide();
$('#header').click(function () {
if (!headerExtIsOpen) {
$('#headerExt').show();
headerExtIsOpen = true;
} else {
$('#headerExt').hide();
headerExtIsOpen = false;
}
});
}
There is no need to remember the state, just use toggle()
$(function () {
setMenu();
});
function setMenu()
{
$('#headerExt').hide();
$('#header').on("click", function (e) {
e.preventDefault();
$('#headerExt').toggle();
});
}
You said you want to toggle other things.
Best thing would be to toggle a class to change the color
$('#header').on("click", function (e) {
e.preventDefault();
$(this).toggleClass("open");
$('#headerExt').toggle();
});
another way is to check the state
$('#header').on("click", function (e) {
e.preventDefault();
var child = $('#headerExt').toggle();
var isOpen = child.is(":visibile");
$(this).css("background-color" : isOpen ? "red" : "blue" );
});
if the layout is something like
<div class="portlet">
<h2>Header</h2>
<div>
<p>Content</p>
</div>
</div>
You can have CSS like this
.portlet h2 { background-color: yellow; }
.portlet > div { display: none; }
.portlet.open h2 { background-color: green; }
.portlet.open > div { display: block; }
And the JavaScript
$(".portlet h2 a").on("click", function() {
$(this).closest(".portlet").toggleClass("open");
});
And there is layouts where it would be possible to have zero JavaScript involved.
Turns out I had some script hidden in my .js file that closes the menu again when the user clicks elsewhere, that I forgot about.
function resetMenu(e) {
var container = $('#headerExt');
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
$('#header').css("background-color", "inherit");
container.hide();
headerExtIsOpen = false;
}
}
I forgot to set the headerExtIsOpen back to false again after closing it in this function (code above shows the fix). Now it works fine :)
I have a button on my page
<button id="show1" class="morelinkblack">Find Out More</button>
When clicked it runs this code
$("#show1").click(function(){
$(".hidden-div1").slideToggle("slow");
});
Is there a way of changing the text to say "Close" instead of "find out more" while the div is visible?
You can do:
$("#show1").click(function(){
var that = $(this);
$(".hidden-div1").slideToggle("slow", function() {
if ($(this).is(":visible")) {
that.text("Close");
} else {
that.text("Find out more");
}
});
});
Based on the comments, the div could be opened or closed depending on where the user is directed from. A simple check in the DOM ready can set the text accordingly:
$(".hidden-div1").is(":visible") ? $("#show1").text("Close") : $("#show1").text("Find out more");
Maybe this :
$("#show1").click(function () {
var $div = $(this);
$(".hidden-div1").slideToggle("slow").promise().done(function () {
$div.text(function () {
return $(this).is(":visible") ? "Close" : "Find Out More"
});
});
});
Updated code
$("#show1").click(function() {
var el = $(this);
$(".hidden-div1").slideToggle("slow", function() {
if ($(this).css('display') == 'none') { // <- Updated here to check if div is close or open
el.text('Find Out More');
} else {
el.text('Hide More');
}
});
});
Update button text on document ready / page load
$(document).ready(function() {
if ($(".hidden-div1").css('display') == 'none') {
$("#show1").text('Find Out More');
} else {
$("#show1").text('Hide More');
}
});