Switch between visibility of objects with toggle button

I have set a background image on the body, and positioned a large box, footer, navigation bar, and header above it.

In order to add functionality to slide these elements up and down, I've created two buttons with classes 'slideUp' and 'slideDown'. The 'slideUp' button features an arrow pointing upwards while the 'slideDown' button has an arrow pointing downwards.

Upon clicking the 'slideUp' button, the box, footer, nav, and header will slide up and disappear. Clicking the 'slideDown' button reverses this action by sliding everything back down.

To toggle between showing the 'slideUp' button and 'slideDown' button based on visibility of elements, I've used an if statement. When all elements are visible, the 'slideUp' button is displayed and the 'slideDown' button is hidden. Conversely, when all elements are hidden, the 'slideDown' button is shown and the 'slideUp' button is hidden.

While the initial transition works smoothly, there seems to be an issue with the 'else if' condition not functioning as expected. After clicking the 'slideUp' button, the 'slideDown' button fails to appear as intended. This page utilizes jQuery for the slideUp and slideDown functionalities.

$("button#Up").click(function() {
 $(".box").slideUp("medium");
 $("nav").slideUp("medium");
 $("header").slideUp("medium");
 $("footer").slideUp("medium");
});

$("button#Down").click(function() {
 $(".box").slideDown("medium");
 $("nav").slideDown("medium");
 $("header").slideDown("medium");
 $("footer").slideDown("medium");
});

if ($(".box").is(":visible") && $("nav").is(":visible") && $("header").is(":visible") && $("footer").is(":visible"))
{
  $("button#Up").show("fast");
$("button#Down").hide("fast");
}
else if ($(".box").is(":hidden") && $("nav").is(":hidden") && $("header").is(":hidden") && $("footer").is(":hidden"))
{
$("button#Up").hide("fast");
$("button#Down").show("fast");
}
button.slideUp{
background-image: url(arrowUp.png);
background-size: cover;
background-position: center;
background-color: #1A1A1A;
opacity: 0.75;
height: 41px;
width: 41px;
position: fixed;
right: 2px;
bottom: 0px;
}

button.slideDown{
background-image: url(arrowDown.png);
background-size: cover;
background-position: center;
background-color: #1A1A1A;
opacity: 0.75;
height: 41px;
width: 41px;
position: fixed;
right: 2px;
top: 0px;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<button id="Up" class="slideUp"></button>
<button id="Down" class="slideDown"></button>

Answer №1

Is this what you were looking for?

$("button#Up").click(function() {
 $(".box").slideUp("medium");
 $("nav").slideUp("medium");
 $("header").slideUp("medium");
 $("footer").slideUp("medium");
 $("button#Down").show();
 $(this).hide();
});

$("button#Down").click(function() {
 $(".box").slideDown("medium");
 $("nav").slideDown("medium");
 $("header").slideDown("medium");
 $("footer").slideDown("medium");
 $("button#Up").show();
 $(this).hide();
});

You can simplify this functionality with just one button, using toggleClass and toggleSlide

$("button").on("click",function() {
 $(this).toggleClass("slideUp slideDown");
 $(".box").toggleSlide("medium");
 $("nav").toggleSlide("medium");
 $("header").toggleSlide("medium");
 $("footer").toggleSlide("medium");
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What purpose do controller.$viewValue and controller.$modelValue serve in the Angular framework?

I'm confused about the connection between scope.ngModel and controller.$viewValue/controller.$modelValue/controller.$setViewValue(). I'm specifically unsure of the purpose of the latter three. Take a look at this jsfiddle: <input type="text" ...

The auto-refresh feature of DataTables is not functioning as expected

Having trouble with the reload feature of DataTables. This is the code I'm using to load and reload the table on the server-side: $( document ).ready(function() { $('#dienst_tabelle').DataTable( { "ajax": "getData ...

What is the process for generating a dynamic array in typescript?

Looking to create a TypeScript dynamic array with the desired format: const display = [ { id: 1, displayName: "Abc1" }, { id: 2, displayName: "Abc2" }, { id: 3, displayName: "Abc3" } ] Attempted the following code ...

Delete a particular table while utilizing $.fn.DataTable.tables()

On a single page, I have several tables that need to be removed when the user decides. My approach was to utilize let table = $.fn.DataTable.tables() table[i-1].destroy(); This code is aimed at obtaining an array of all the tables and subsequently destroy ...

Implement the AngularJS orderby filter based on a checkbox selection

Is it possible to use the angularJS orderby filter with a checkbox for ordering columns? I currently have this working as expected: <tr ng-repeat="player in players | orderBy:'id':true | rangeFilter:min:max"> <td>{{player.id}}</ ...

Troubleshooting: jQuery Orientation Change Issue Unresolved

Below is the code snippet I am using: jQuery(window).on("orientationchange",function(){ var maxHeight = -1; jQuery('.article .articleHead').each(function() { maxHeight = maxHeight > jQuery(this).height() ? maxHeight : jQuery(this). ...

Crushing jQuery's Sortable/Droppable

Having a little issue here. I want to be able to toggle the sortable plugin's behavior by clicking a button - basically switching between sort mode and view mode. I've attempted to achieve this with the following code: function enterSortMode(){ ...

Modifying all occurrences of a specified string in an object (or array) - JavaScript

Is there a more efficient way to search through and replace all instances of a given string in a JavaScript object with unknown depth and properties? Check out this method, but is it the most optimal solution? var obj = { 'a' : 'The foo ...

Breeze js requests metadata without needing to make a second call to the server

Currently, I am developing an Angular application that utilizes Breeze JS and ASP.NET OData controller. While testing, I encountered an issue where Breeze JS successfully makes the initial call to retrieve metadata from the server but fails to make the sec ...

The system does not acknowledge NPM as a valid internal or external command

Here is the content of my package.json file for the server directory, which I am attempting to launch: { "name": "server", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "nodemon src/app.js --exec 'npm ...

Performing an Ajax request using MooTools when my button is clicked

After clicking a button, I want to initiate an ajax call. There are more than 14 buttons on my site that make ajax requests to fetch elements from various parts of the site. Button, Button1, Button2, Button3 | | | load content | | ...

Utilizing Promise and setTimeout in a generator function to develop an asynchronous generator: A comprehensive guide

I am currently working on creating a generator function that produces an async generator. This Generator will yield a deferred value at each time, using promises. The values and their respective delays (in milliseconds) are sourced from two separate arrays ...

Detecting the Escape key when the browser's search bar is open - a step-by-step guide

One feature on my website is an editor window that can be closed using the Escape key. The functionality is implemented in JavaScript: $(document).keyup( function(e) { // Closing editor window with ESCAPE KEY if(e.which == 27) { // Clic ...

Preventing AngularJS from Ignoring HTML Elements

Is there a way to calculate HTML content within an AngularJS object (such as {{names}}) that includes an '<a>' element? When I try to display it, the result looks like this: <a href="http://www.example.com">link text</a> I&ap ...

Switch image upon hover within a sprite

I have a sprite that I utilize for displaying various images. Currently, my aim is to change the sprite image upon hovering using solely CSS. .sprE { background-color: transparent; background-image: url(/i/fW.png); background-repeat: no-repeat; height: 30 ...

Steps for removing a p5.js instance once three.js assets have finished loading

I am trying to implement a preload animation using a p5 sketch while loading a three.js gltf file onto my webpage. The idea is to have the p5 animation play while the heavy gltf file loads in the background. However, I am facing issues with triggering the ...

Improving the display of events with fullcalendar using ajax requests

I have integrated the fullcalendar plugin from GitHub into my project. I am looking to implement a feature where I can retrieve more events from multiple server-side URLs through Ajax requests. Currently, the initial event retrieval is functioning proper ...

What is the reason for the regeneration of the 2D array?

I have a method called generateWeights() that retrieves random values in an array; And a method named learn() that calls the changeWeights() method to alter the values in the array. Expected: Prior to invoking the learn() method, I anticipate receiving an ...

What is the best way to populate dropdown menus using JavaScript?

I'm facing an issue with my ajax request where I am unable to populate the options of a select tag. This problem is occurring in multiple blocks where the select tag serves the purpose of choosing a type of product. Here is how my select tag looks li ...

Tips for aligning one item in the center and another item on the right with MUI v5

My goal is to center 3 navigation tabs in the middle of my page and have a dropdown on the far right for sorting. I managed to get the dropdown on the far right, but I'm having trouble perfectly centering the 3 navigation tabs inside the <Box> & ...