Is there a way to adjust the slide length in JQuery's slideUp function using pixels? It seems like

At the bottom of a page, I have a fixed position div element. There is another div below it, but it is hidden outside of the page margins. I am attempting to slide both divs upwards so that the bottom one becomes visible (moving out of the margin). However, the JQuery code for this is a bit confusing - using slideUp makes it go down, and I am unsure how to determine the slide length in pixels.

$(document).ready(function() {
  $('#butt').click(function() {
    $('#messbar').slideUp();
    $('#botbar').slideUp();
  });
  $('#reset').click(function() {
    $('#messbar').slideDown();
    $('#botbar').slideDown();
  });
});
#container {
  height: 600px;
}
#botbar {
  position: fixed;
  margin: auto;
  height: 20px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  background-color: red;
  text-align: center;
}
#messbar {
  position: fixed;
  margin: auto;
  height: 20px;
  bottom: -20px;
  left: 0px;
  right: 0px;
  background-color: blue;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
  <button id="butt">Slide</button>
  <button id="reset">Reset</button>
  <div id="botbar">
    text
  </div>
  <div id="messbar">
    more text
  </div>
</div>

This is the current state. Here is the desired appearance after clicking "slide":

    #container {
      height: 600px;
    }
    #botbar {
      position: fixed;
      margin: auto;
      height: 20px;
      bottom: 20px;
      left: 0px;
      right: 0px;
      background-color: red;
      text-align: center;
    }
    #messbar {
      position: fixed;
      margin: auto;
      height: 20px;
      bottom: 0px;
      left: 0px;
      right: 0px;
      background-color: blue;
      text-align: center;
    }
<div id="container">
  <div id="botbar">
    text
  </div>
  <div id="messbar">
    more text
  </div>
</div>

After resetting, the divs should return to their initial position as shown in the first snippet.

When "slide" is clicked, the #botbar and #messbar should both slide up by 20px. On "reset" click, they should slide back down by 20px.

Any suggestions on how to achieve this?

Answer №1

$(document).ready(function() {
  $('#button').click(function() {
    $('#message-bar').animate({bottom:0});
    $('#bottom-bar').animate({bottom:$('#message-bar').height()});
  });
  $('#reset').click(function() {
    $('#message-bar').animate({bottom:-($('#message-bar').height())});
    $('#bottom-bar').animate({bottom:0});
  });
});
#container {
  height: 600px;
}
#bottom-bar {
  position: fixed;
  margin: auto;
  height: 20px;
  bottom: 0px;
  left: 0px;
  right: 0px;
  background-color: red;
  text-align: center;
}
#message-bar {
  position: fixed;
  margin: auto;
  height: 20px;
  bottom: -20px;
  left: 0px;
  right: 0px;
  background-color: blue;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
  <button id="button">Slide</button>
  <button id="reset">Reset</button>
  <div id="bottom-bar">
    text
  </div>
  <div id="message-bar">
    more text
  </div>
</div>

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

Troubleshooting Problem with Creating a Button using HTML Checkbox "onclick" Event

My main goal with the checkbox click event is to achieve the following objectives: 1] Create a button with the id = 'id-of-checkbox'+'some-character-here' in a specific div. 2] Clicking on that button will remove both the button and ...

Creating dynamic charts in Javascript using Firebase

My dad recently asked me to enhance our motor home by integrating an Arduino with Firebase to monitor the water and propane tanks. He's hoping to be able to check tank levels from his phone so he can see when they need refilling. I've successful ...

Guide on inserting the elements <label>, <input>, and <span> into a <li> in a particular sequence

var btn = document.querySelector('.add'); var remove = document.querySelector('.draggable'); function dragStart(e) { this.style.opacity = '0.4'; dragSrcEl = this; ...

Managing and Streaming Music in a Rails Application

Currently, I am storing mp3s using paperclip and they only play back if I utilize Amazon S3. However, I am hesitant to use Amazon S3 because I am unsure how to secure the files. Now, I am reconsidering my approach as I need to secure the mp3s and provide ...

How about incorporating a unique twist while utilizing a for loop to attach click listeners?

I've been exploring, but I'm unable to find a solution to this particular issue. I am working with a linked list that needs to be accessed when specific items on my webpage are clicked. To do this, I created a get(id) method that iterates throug ...

Ajax encountered an unexpected TypeError: Unable to redefine property:

Seeking assistance with a problem I am facing when running an AJAX request code to populate a select box based on the user's selection from another select box: $("#item_type").change(function () { var item_type = $("#item_type").val(); if(item_type ...

Customize the appearance of Angular components by altering their color schemes

When working on my project, I often utilize pre-made Angular components. However, some of these components come with colors that do not align with the overall aesthetic of my project (refer to image above). To rectify this issue, I am looking to replace t ...

Is it advisable to set the html root at 62.5% when using rem units in CSS? Does this imply that the default font size of browsers is 16px?

During a discussion with a group, I mentioned the 62.5% trick for scalability (setting 62.5% as font size in root and using rem units throughout CSS for easier scalability when users change default browser font size) and their arguments were as follows: " ...

Concealing a hyperlink depending on the value chosen in a dropdown menu

Looking for guidance on how to use jQuery to read the current value of a drop-down list and hide a specific link based on that value. The drop-down list is for selecting the language/locale. For example, when "English" is selected, I want the link to be h ...

Troubleshooting issue with ASP.NET AJAX button not triggering

When using AJAX for product search, I encountered an issue with the pagination functionality not firing after receiving the search results in the ShopAjaxSearch.aspx file. ShopAjax.aspx : <div id="demo-2"> <input type="search" placeholder="S ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

AngularJS featuring a dropdown selector

When I choose an option from the dropdown menu, I want to display the corresponding table from the SQL database. Below is the code for this functionality: If you select an option from the dropdown and press the button, the table associated with that option ...

Dynamic content popup using jQuery and Ajax technology

In my Rails application, I have multiple rows of data displayed on a page. For each row, I want to implement a button that will reveal additional data in a popup specific to that row. After researching possible solutions, here's what I found: jQu ...

Obtaining information from a database and integrating it into introjs utilizing JSON

Currently, I am using IntroJs and JSON to call the introjs. I am wondering if it is possible to store data/text/info in a database and then retrieve it using JSON? <h2 id="welcomeMsg">Welcome back,<asp:Label ID="lbl_WelcomeName" runat="server" Te ...

What is the best way to retrieve widget options when inside an event?

Creating a custom jQuery widget: jQuery.widget("ui.test",{ _init: function(){ $(this.element).click(this.showPoint); }, showPoint: function(E){ E.stopPropagation(); alert(this.options.dir); } } Initializing the cu ...

Certain CSS styles for components are missing from the current build

After building my Vue/Nuxt app, I noticed that certain component styles are not being applied. In the DEVELOPMENT environment, the styles appear as expected. However, once deployed, they seem to disappear. All other component styles render properly. Dev ...

Discovering the PHP error message that is sent to AJAX through JQuery

This is the code snippet that works: $.ajax({ type: 'POST', url: 'register.php', data: {captcha: captcha }, success: function() { $('#loading').hide() ...

What is the best way to have my program switch out a string with the pressed key in a particular portion of the string?

I'm currently developing a hangman game using JavaScript. I am facing an issue where I need to replace the "-" with the guessed letter by the user in the correct position it belongs to within the word. While I can add the letter at the beginning or en ...

Error encountered when attempting to retrieve HTML content from localhost using AJAX

Attempting to insert HTML code into a div element using ajax, similar to how it's done with an iframe. Testing this out locally first to avoid Same Origin Policy complications. The web application is currently hosted on a wamp server. There are two ...

Examples of merging responsive design and equal height columns in CSS:

As a beginner in CSS, I am facing challenges in creating a responsive layout that adjusts based on the screen it is viewed on and achieving equal heights in columns. While I have been able to address these issues individually, combining them has proven to ...