Interactive feature: Div partially revealed, slides up upon image click

Here's a unique request - I'm trying to create an effect where a div slides up from the bottom of the screen when someone clicks on an image. To give you a better idea, think of the Windows desktop: when you click on the start menu icon, instead of the menu popping up from below, the entire menu bar slides up revealing the full div.

Currently, with my limited knowledge of JS and jQuery from codecademy, I am using the slideUp function. However, this is causing the div to slide down and out of view instead of up as intended. The goal is for the div to slide up when the button is clicked, and if the button is clicked again (or anywhere outside the div), it should slide back down, leaving the top 60px visible just like before.

Take a look at my code:


$('#start').click(function() {
$('#nav').slideUp('slow');
});

HTML:


<div id="nav" class="nav">
    <img id="start" src="img/btn_start.png">
</div>

CSS:


* {
    padding: 0px;
    margin: 0px;
}

body {
    width: 100%;
    font-family: Helvetica;
}

.nav {
    width: 100%;
    min-width: 100%;
    height: 500px;
    background: #000;
    color: #fff;
    position: absolute;
    bottom: -440px;
    text-align: center;
    padding: 5px;
    box-sizing: border-box;
    overflow: auto;
 }

.nav ul li {
     display: inline;
}

.nav li {
     padding: 20px;
     margin-top: 80px;
     position: relative;
     top: 50%;
     transform: translateY(-50%);
 }

 #start {
    float: left;
 }

I appreciate your help in making this work smoothly without any hiccups!

Answer №1

Try using animate instead of slideUp:

$('#start').click(function() {   
    $('#nav').animate({bottom: "0px"}, 1200);   
});

This will smoothly animate the element to align its bottom with the containing element's bottom.

For even smoother animations, consider using velocity.js (), which syncs animation with browser frame updates for optimal performance.

Check out the JsFiddle demonstration here: http://jsfiddle.net/11r46jnm/

You can also achieve this effect using CSS transitions by leveraging HTML data attributes:

<div id="nav" class="nav" data-nav-state="collapsed">
    <img id="start" src="img/btn_start.png">
</div>

Control the state change with JavaScript:

$('#start').click(function() {
    var currentState = $('#nav').attr("data-nav-state");
    var newState = "collapsed";
    if (currentState === "collapsed") {
        newState = "expanded";
    } 
    $('#nav').attr("data-nav-state", newState);
});

Define the positions and transitions in CSS for smooth effects:

#nav[data-nav-state=collapsed] {
      bottom: -440px;
 }

 #nav[data-nav-state=expanded] {
      bottom: 0px;
 }

 #nav {
      transition: bottom 1.2s ease;
 }

View the demo on jsFiddle: http://jsfiddle.net/Lv2saepy/1/

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

Showing a level based on quiz scores in a JavaScript view

Attempting a small Vue JS project, I've hit a roadblock near the finish line. My goal is to display different levels in the browser based on the score achieved in a quiz. Despite finding some hints, I'm encountering issues when trying to execute ...

When transferring an object from a blade template to a Vue component through props, it may become undefined

I'm facing an issue where I am attempting to send an array from an asynchronous method in the controller to a Vue component using props within a blade template. However, when I try to log it in the Vue component, it shows up as undefined. Here is the ...

Adding text can only be done to the initial list item

I have a menu structured like this: <ul class="nav"> <li class="dropdown" id="123"> <ul class="nav123"> <li id="widget-products"> <ul class="123">// content populate ...

When the CKEDITOR is set to fullPage mode, it cannot properly apply styles from an external stylesheet that is configured in the config.contentscss setting

To create a custom StyleSet in CKEditor using styles from an external stylesheet, I configured the settings as follows: config.extraPlugins = 'stylesheetparser'; config.contentsCss = '/css/externalStyleSheet.css'; config.stylesSet = [ { ...

What are the best techniques for displaying high-resolution images on mobile devices and lower-resolution images on desktop computers?

I implemented a typical bootstrap grid to display a row of images, with 4 images in desktop view and 1 image in mobile view. Here is what it looked like: <div class="row"> <div class="col-md-3 col-sm-12"> <img ...

Is there a rationale behind using intricate CSS classes such as page-left-column-container-box-element-header for technical purposes?

What is the reasoning behind using this specific CSS and HTML structure for styling web pages? <div class="page"> <div class="page-left-column"> <div class="page-left-column-container-box"> <div class="page-left-column-con ...

Ways to update images without having to reload the page following an ajax request

My current script uploads images to the server and updates the image source in HTML upon completion. However, I am facing an issue with my PHP code renaming the files to predefined names (ext-pix0.png, ext-pix1.png, etc.) during upload. This leads to the p ...

Can I switch between different accounts on Auth0?

While utilizing next-auth for user sign-ins, I've noticed that there isn't a clearly visible option to switch accounts after signing in. Additionally, if users enter incorrect credentials, there doesn't seem to be a way to sign in with a dif ...

Limiting the space character in a password field with regular expressions in JavaScript

Currently, I am facing the challenge of implementing a RegEx expression on a property within a model on an MVC website that I am currently developing. While I understand the individual components of the expression, I am struggling to combine them effectiv ...

What is the best way to display data stored in local storage using React and Typescript?

Is there a way for the data to be displayed on the browser below the save button when I click save? My setup involves using React with a TypeScript template. function ButtonDefaultExample(props: IButtonExampleProps) { const { disabled, checked } = pro ...

Steps for transmitting a file through an AJAX call without including form data

Can anyone help me figure out how to send a PDF file from ajax to communicate with a Django server, without using form data? ...

Tips for breaking out of the loop in Nightwatch when using the '.elements' method

As someone who comes from the Java world, I am finding it a bit challenging to work with Nightwatch. I have a use case where I need to get a list of all ids on a page and if a certain id matches, click on it and exit the loop. However, I've tried vari ...

Javascript failing to choose background color from an array

I am attempting to create a subheader with varying colors, similar to the one found on the Ask Different page. However, instead of manually assigning colors, I am looking to have it randomly select a color from a Javascript array. I have already outlined ...

Guide to making a vertical clickable separator/line using bootstrap

Seeking advice on creating a layout where the left side consists of a sidebar menu occupying 24% and the right side contains main content taking up 75%, with a clickable vertical divider at 1% between them. When this line is clicked, the left part will hid ...

Move information from a spreadsheet into an online form

Looking to transfer data from a sheet to a dropdown in a form using Google Script. The data has been gathered in a table and I believe transferring it through a JS script is the best solution. (Let me know if you have a better option) Some tests have been ...

How come my image isn't cooperating with text-align and vertical-align?

Hey everyone! I'm a newcomer to this community and a newbie when it comes to HTML. :D I encountered a problem recently and discovered that Stackoverflow is full of amazing developers. That's why I've decided to share my problem here and am ...

Display the form-control field only when a selection has been made in the previous form-control using HTML and PHP

I'm in the process of developing a webpage with form controls (2 in this example). Here's my code: <body> <div class="option_choice_global"> Select your options <div class="col-xs-2"> <lab ...

Data not being saved when using the Post method in a Web API

I built a straightforward Web API application that allows users to GET or POST data. The data consists of a simple array of Strings like ["foo", "bar"]. However, when I try to send data using the POST method to the Web API and then make another call to ret ...

Activate the click function of an asp.net linkbutton when the mouse enters by utilizing jQuery

I'm attempting to create a hover-triggered click event using jQuery. While this is a straightforward task, I've run into an issue where I can't seem to trigger the click event of an ASP.NET link button that refreshes the content of an updat ...

Utilizing jQuery for extracting information from JSON datasets

I've been grappling with getting jQuery to interpret JSON data, but it keeps coming back as undefined. $.post( wce_data.url, { userid : userId, token : accessToken }, 'json') .done(function( data ) { consol ...