Upon clicking on a different div, a div will elegantly slide down from above the footer

I'm struggling with sliding a div from the bottom of the page.

Here is my JSFIDDLE

Currently, when I click on the blue box (arrow-hover), the div slides up (box-main).

However, to hide the div (box-main) again, I have to click inside the box-main div.

My goal is to hide the div when I click on the blue box (arrow-hover) again. I've tried various methods but without success.

Additionally, I used position fixed for both boxes so that when zooming in on the page, the box remains fixed and covers the rest of the page. If I use position absolute or relative, the div (box-main) is visible below the footer, which is not ideal. Is there another way to achieve this?

Here is my code:

HTML:

 <div id="container">
<div id="box-main"></div>
<div id="arrow-hover"></div>
    <footer></footer>
</div>

CSS:

    #container
{
    margin:0;
    width:100%;
    height:100%;
}

#box-main{
    position: fixed;
    left:150px;
    bottom: -150px;
    width: 250px;
    height: 150px;
    background: #000;
    z-index: 100;
}
#arrow-hover{
    position: fixed;
    bottom: 50px;
    left: 250px;
    width: 50px;
    height: 50px;
    background: blue;
    z-index: 100;

}
footer
{
    width:100%;
    background:green;
    height:50px;
    bottom:0;
    left:0;
    position:absolute;
    z-index:500;
}

Jquery:

 $('body').on('click','#arrow-hover',function(){
    $('#arrow-hover').animate({
        bottom: '200px'
    },250);
    $('#box-main').animate({
        bottom: '50px'
    },250);
});



$('body').on('click','#box-main',function(){
    $('#arrow-hover').animate({
        bottom: '50px'
    },250);
    $('#box-main').animate({
        bottom: '-150px'
    },250);
});

Additionally, I would like to add an upward arrow in the (arrow-hover) div so that when clicked, the div moves up and the arrow points downwards. When clicked again, the div should hide. Can this be achieved using JQuery, JavaScript, or CSS3?

Any assistance would be greatly appreciated. Thank you!

Answer №1

Instead of depending on variables, I simply added a class to your main box called "visible".

This class is toggled in both animations - it is added when you show the box and removed when you hide it:

Update: To include some arrows in the clickable div, we added an "arrow up" class to the #arrow-hover element in the HTML:

<div id="container">
  <div id="box-main"></div>
  <div id="arrow-hover" class"arrow-up"></div>
  <footer></footer>
</div>

Then, we implemented the toggleClass function in your arrow-hover click handler to switch between the arrow-up and arrow-down classes.

$('body').on('click','#arrow-hover',function(){
if($('#box-main').hasClass("visible"))
{
    $('#arrow-hover').animate({
        bottom: '50px'
    },250).toggleClass("arrow-up arrow-down");
    $('#box-main').animate({
        bottom: '-150px'
    },250).toggleClass("visible");
}else
{ 
    $('#arrow-hover').animate({
        bottom: '200px'
    },250).toggleClass("arrow-up arrow-down");
    $('#box-main').animate({
        bottom: '50px'
    },250).toggleClass("visible");
}
});

Now, all that's left to do is define the arrow-up and arrow-down classes in your CSS and set a background image.

For this purpose, I used the icon font "Font Awesome" http://jsfiddle.net/hapttwf6/28/ --> This is how it looks like ;)

Answer №2

To toggle the visibility of a div, you can use a boolean variable to track its state and show or hide it accordingly.

Check out the updated fiddle here

var isClicked = false;

$('body').on('click','#arrow-hover',function(){
    if(!isClicked){
        $('#arrow-hover').animate({
            bottom: '200px'
        },250);
        $('#box-main').animate({
            bottom: '50px'
        },250);
        isClicked = true;
    }
    else{
        $('#arrow-hover').animate({
            bottom: '50px'
        },250);
        $('#box-main').animate({
            bottom: '-150px'
        },250);
        isClicked = false;
    }
});

I hope this solution proves useful for your needs.

Answer №3

Check out my updated code snippet:

$('body').on('click','#arrow-hover',function(){
    $('#arrow-hover').animate({
        bottom: '200px'
    },250);
      $('#box-main').animate({
        bottom: '50px'
    },250);
    if($('#arrow-hover').css('bottom')!='50px'){
    $('#arrow-hover').animate({
        bottom: '50px'
    },250);
        $('#box-main').animate({
        bottom: '-150px'
    },250);
    }
});
#container
{
    margin:0;
    width:100%;
    height:100%;
}

#box-main{
    position: fixed;
    left:150px;
    bottom: -150px;
    width: 250px;
    height: 150px;
    background: #000;
    z-index: 100;
}
#arrow-hover{
    position: fixed;
    bottom: 50px;
    left: 250px;
    width: 50px;
    height: 50px;
    background: blue;
    z-index: 100;

}
footer
{
    width:100%;
    background:green;
    height:50px;
    bottom:0;
    left:0;
    position:absolute;
    z-index:500;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="container">
<div id="box-main"></div>
<div id="arrow-hover"></div>
    <footer></footer>
</div>

Visit my fiddle for more details

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

Tips on maintaining the numerical value while using JSON.stringify()

Having trouble using the JSON.stringify() method to convert some values into JSON format. The variable amount is a string and I want the final value in JSON format to be a number, but it's not working as expected. Even after applying JSON.stringify(), ...

What steps are necessary to alter the background color of a jumbotron?

Can someone help me figure out how to change the background-color of the 'jumbotron' class in Bootstrap? The default color set in bootstrap.css is #eee. I've tried various methods like replacing it with none, none !important, or transparent ...

Ways to highlight a form field only when there is data in my variable

For the validation of email addresses in my form, I have been using a combination of php and ajax. The process involves sending the email value via jQuery to my php file and displaying a message if the email is found in the database. While my code function ...

Removing an Element in a List Using Jquery

In my JQuery, there is a list named additionalInfo which gets populated using the function below: $('#append').on('click', function () { //validate the area first before proceeding to add information var text = $('#new-email&a ...

Utilizing Javascript to Generate a Comma-Separated List from Multiple Select Elements

I have a set of dynamic single-option select elements that I need to work with. My goal is to generate a list containing the indexes of all selected options, separated by commas. Currently, I am using elements = document.getElementsByClassName("my-class ...

Vue JS - Unable to locate module (datepicker)

Today marks my first time delving into the world of vue.js, and as expected, I've encountered an error that has me stumped. I recently incorporated the v-md-date-range-picker module into my project: (. The setup instructions provided me with the f ...

What is the correct way to denote a CSS class in SASS syntax?

Can you show me how to code this in SASS? .solo:hover > .solo-pane, .solo-pane.active, .solo-pane.lighten { opacity: 0.5; } I'm having trouble separating the classes using commas. ...

I'm seeking assistance in enhancing this code within tb for extracting values using JavaScript

Currently, I am extracting values from a table using JavaScript. The code seems to be functioning correctly, but I am looking for ways to optimize it and ensure that it is compatible with most modern browsers. The list in the table undergoes frequent chang ...

Trouble organizing a collection of destinations based on their geolocation and proximity

To address this issue, my approach has been to feed variables into the value attributes of an option list and then sort it based on these values. When manually assigning values, everything works smoothly, for example: <option id="link1" value="1">A& ...

What are the steps for sending information to a PHP file and retrieving the outcome with AJAX?

Hey there! I'm looking to send data to PHP, receive the PHP result, and then display the result in an HTML div tag. Could you provide me with a sample code snippet for this? Thank you in advance and please excuse any mistakes in my English. ...

Is it possible to determine if the process for changing Three.js material has been successfully completed?

Is it possible to determine the completion of this process? material.map = new THREE.Texture( canvas ); material.map.needsUpdate = true; If not, sometimes the final result will be a black snapshot. var snapshotData = renderer.domElement.toDataURL(strMi ...

The code snippet for the carousel in Bootstrap does not function correctly in VSCode, yet it operates effectively on codeply.com

code: <div class = "carousel-inner"> <div class = "carousel-item active" style = "background-color:red" > <h2>I don't have to sniff other dogs anymore for love. Thanks to TinDog, ...

Experiencing difficulty transmitting information from the view to the controller within CodeIgniter

I am currently creating a website using the CodeIgniter framework. One of the features on my site is a view that displays unverified members in a table. Each member has a button next to their name that can be clicked to verify their account. Below is the ...

I am having trouble understanding why dropdown menus with the same content have varying widths

My issue is illustrated in the screenshots below: first-dropdown: second-dropdown: The second dropdown appears slightly wider on the right side. Despite Google Chrome's claim that the widths are the same. HTML code: <div class="row menu"> ...

Button click initiates DataTables search rather than manually entering text in the input field

I am exploring the option of relocating the search function from an input to a button for a table that has been modified using DataTables. Currently, I have a customized input that triggers this function: <script> $(document).ready(function() { ...

Table placement within a cell of a table

Seeking assistance with an issue I encountered, where the date of 12th March is combined with a table. Any suggestions on how to separate them? Screenshot: UPDATE:JFIDDLE ...

Assistance with utilizing the jQuery .html() function

Presently, I am executing the following code snippet: <script type="text/javascript"> $(function(){ $("#ipad").submit(function() { $.post("ipadcheck.php", $("#ipad").serialize(), function(data) { if(data.error == 'TRUE& ...

How to stop event propagation from a draggable element nested within a sortable container in jQuery UI

Here is a link to the code snippet: http://jsfiddle.net/Aa7TW/ {greedy: true} The issue arises with the interaction between droppable and sortable elements. Sortable is triggered when an item is dropped directly onto it, while dropping an item from the ...

What is the best way to separate two <a> tags using only Bootstrap?

Currently, I am utilizing a PHP for loop to display 10 tags on an HTML page. My challenge lies in wanting the <a> tags to be displayed on separate lines with some spacing. It's common practice to handle this through custom CSS, however, is ther ...

Enhance Your Website with Dynamic Mapping Using Google Maps Version 3 API and Select/Dropdown Elements

Hey there! I am looking to create a dropdown menu that lists various countries. When a user selects a country from the dropdown, I want a Google Map to automatically zoom into that specific location. For example, if someone picks 'Brazil' from t ...