Tips for concealing a div outside the viewport while ensuring the bottom 50px always remains visible

I am trying to achieve an effect where I can move a div off the screen (top up) while keeping the bottom edge visible, specifically 50px. Currently, my approach involves hiding 90% of the div using jQuery and CSS3 with jquery.transit for cross-browser compatibility.

The issue arises when the content inside the div is dynamic and changes its height, leading to unexpected transformations and discrepancies in the percentage positioning across different viewports (e.g., iPhone 4s - Chrome Desktop browser), despite setting minimum heights. The div needs to be responsive and not static.

I may be missing something minor, but how can I resolve this problem to achieve the desired outcome as shown in the linked fiddle without relying on a fixed height?

HTML

<button id="left">&laquo;</button>
<button id="right">&raquo;</button>
<div class="block">
    <p id="text">Test</p>
</div>

JS

$( "#right" ).click(function() {
  $( ".block" ).animate({ "y": "-85%" }, "slow" );
});

$( "#left" ).click(function(){
  $( ".block" ).animate({ "y": "0%" }, "slow" );
});

CSS

body{
    100%
}
p{
    bottom:0;
    left:50%;
    position:absolute;
    text-align:center;
    color:white;
}
.block {
    width: 100%;
    min-height:150px;
    border: 1px solid black;
    margin: 0 auto;
    position: absolute;
    background-size: cover;
    background-color: gray;
    z-index: 2;
}
#left {
    position:absolute;
    z-index:3;
}
#right {
    left:10%;
    position:absolute;
    z-index:3;
}

fiddle

Answer №1

To begin, measure the height of the element with the class .block, subtract 50 from it, and then animate its position. The following example demonstrates this functionality without utilizing the min-height property on the .block class:

https://jsfiddle.net/hw14kkoc/5/

$( "#right" ).click(function() {
  var y_delta = $(".block").height() - 50
  $( ".block" ).animate({ "y": "-" + y_delta + "px" }, "slow" );
});

$( "#left" ).click(function(){
  $( ".block" ).animate({ "y": "0px" }, "slow" );
});

This approach ensures that regardless of the height of the element with the class .block, it will always move up enough to leave just 50px visible.

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

Step-by-step guide on loading the AngularJS app with the jQuery load function

I currently have three different HTML applications: http://localhost/apphost/app1/index.html (contains tables) http://localhost/apphost/app2/index.html (utilizes simple AngularJS) http://localhost/apphost/app3/index.html (uses AngularJS with routes) In ...

The Challenge of AJAX and Postback Interactions

I have spent the entire weekend struggling with unwanted postbacks. The popup I am working on contains a button that triggers several stored procedures and then closes the popup window. My goal is to achieve the same result when users click the X close bu ...

Is it possible to utilize AngularJS ngAnimate to perform cross-fading transitions on list items?

I am currently exploring the ins and outs of angularJS with my simple message ticker example. This ticker displays messages by toggling the CSS display property of one of the li elements. <div id="ngtickerMessage" class="ngtickerMessage"> ...

Scrolling text blocks on mobile devices

When viewing the website on a desktop, everything works perfectly. However, when accessing it on a mobile device and trying to scroll down, only the text moves while the page remains stationary. The website utilizes skrollr core for animations. I have alre ...

Employing JavaScript to transfer a string to a Servlet, and then returning the results from the Servlet back to JavaScript

Firstly, I apologize for any errors in my grammar. English is not my first language, but I will do my best to explain my issue clearly. I am currently developing a web application where users can input a link. (Question 1) This link needs to be sent to a ...

Dealing with Three.JS, Amazon S3, and the challenges of access control origin errors in hosting JavaScript files

Overview In order to showcase models stored on Amazon S3, we rely on the use of the Three.JS Javascript library for visualization. For loading models, I exclusively utilize the JSONLoader due to its comprehensive toolchain support. Other formats such as ...

Incorrect character set used in jsonp ajax requests

My ajax request implementation looks like this: function apiCall(resource, data, callback) { if(data == undefined || data == null) data = {}; $.ajax({ dataType: 'jsonp', data: data, url: nodeUri + "/" + resource }). ...

How to toggle classes on anchor tags using jQuery for Twitter Bootstrap buttons?

I built a single-page website. The navigation is made up of buttons styled with bootstrap 3. Since it's a single page, the buttons are anchor tags. I'm trying to figure out how to apply an .active class to the last clicked anchor tag. Here is t ...

Uncertain about the best way to utilize an observable

I am currently developing a real-time chat application using Angular and Node.js. The backend is powered by nodejs and I am utilizing socket.io for communication. Server Side io.on('connection', (socket) => { socket.on('new-message&apo ...

Create a vibrant stroke and conclude with a circular marker beneath the content with CSS

Can you create a vibrant line with a dot underneath text using CSS, similar to the example shown in the image? The underline should begin with some spacing or padding. .underline { text-decoration: underline; text-underline-offset: 10px; displa ...

Tips for handling the accent mark (diacritic mark)

My primary language is Spanish, which means I use accent marks quite frequently (á, é...). When I need to type them out, I resort to using &aacute;, &eacute;, and so on. However, I'm facing an issue when trying to compare two sentences in m ...

The script from 'URL' was declined for execution due to its MIME type of 'text/html' which is non-executable, in addition to strict MIME type checking being enabled

I encountered an error stating "Refused to execute script from 'URL' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled." The code that triggered this error is shown below. <!DOCTYPE htm ...

Exception triggered by attempting to access resources from a different

I recently uploaded my wordpress blog at this URL: However, I am encountering a cross-domain exception when trying to call the following URL: $.ajax({ type: "POST", url: "http://app.blog ...

Not all dynamic content is loaded through Ajax requests on a single domain

I currently have my domain forwarded (cloaked) to The main page (index.html) utilizes Ajax for loading content. All content loads properly at the original host (moppy.co.uk/wtcdi). However, on the forwarded domain (whatthecatdragged.in), some content fai ...

Is there a way to pull information from a string and organize it into a two-dimensional array?

Utilizing the axios library, I am pulling data from a website. Unfortunately, the data being fetched is in HTML format. The extracted data looks like this: 1 Agartala VEAT 120830Z 23004KT 5000 HZ SCT018 SCT025 34/27 Q1004 NOSIG= 2 Ahmedabad VAAH 120830Z 23 ...

Having trouble with applying the height property using Jquery .css to a div element?

I'm currently working on a website project and I'm still new to using jquery. I encountered an issue where the .css() function does not seem to change the height of a specific div, although it works fine for another div on the same page. You can ...

Guidelines for aligning a form in the middle of the screen (positioned between the navigation bar and footer

Before asking this question, I made sure it's not a duplicate by researching previous similar issues with no success. I'm currently utilizing Bootstrap 4. You can view my app at (I'm unable to post the CSS and HTML of my React app due to S ...

Display or conceal a div based on the size of the screen using HTML and CSS

Hey there, I recently finished my first React Project and I’m wondering if there’s a way to hide the 'side-menu' section on mobile screens using CSS. Any suggestions? <div className='side-menu'> <SiderComponent /> < ...

Locate the storage destination of a NodeJS module that has been installed

Examining the following code snippet from the grunt-template-jasmine-istanbul repository on GitHub; var DEFAULT_TEMPLATE = __dirname + '/../../../../grunt-contrib-jasmine/tasks/' + 'jasmine/templates/DefaultRunner.tmp ...

Issue with Chart JS: Firefox is reporting that the 'Chart' is not defined

As a newcomer to Chart JS, I am currently utilizing it in Angular JS version 1.5.3 with Chart JS version 2.1.4. Below is the code snippet I am working with: var myChart = new Chart(ctx, { type: 'line', data: { labels: dates, datasets: [{ ...