Change the position of a Div by clicking on a different Div using JQuery for custom movements

I have successfully managed to slide the div left/right based on the answers below, but I am encountering some issues with the top div. Here are the specific changes I am looking to make: 1. Make both brown lines thinner without affecting the animations. 2. Slide the div left/right when clicking on the vertical brown line and resize the width of the top div accordingly. 3. Slide the top div up/down when clicking on the horizontal brown line. I have made some progress with this but it's not perfect. Can someone please assist me further?

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="JQuery.js"></script>
<script src="JQuery-UI.js"></script>
<%-- <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>--%>
<link href="App_Themes/Design.css" rel="stylesheet" />
<link href="App_Themes/PageDesigns.css" rel="stylesheet" />
<title></title>
<script type="text/javascript">
    $(document).ready(function () {
        $("#showHideDiv").click(function () {
            if (!$('#divMenu').hasClass("off")) {
                $('#divMenu').animate({ 'margin-left': '-95%' }, 500);
                $('#divMenu').addClass("off");
                $('.div3').animate({ 'left': '-10%', 'width': '99.3%', 'margin-left': '-10.5%' }, 500);
                //$('.div3').animate({ 'left': '-10%', 'width': '99.3%', 'margin-left': '-10.5%' }, 500);

            }
            else {
                $('#divMenu').animate({ 'margin-left': '0px' }, 500);
                $('#divMenu').removeClass("off");
                $('.div3').animate({ 'left': '-0%', 'width': '89%', 'margin-left': '-0%' }, 500);
            }
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
    <div style="float: left; width: 11%; height: 850px; padding: 0; margin: 0;">
        <div id="divMenu" class="">
            Sidebar- Menu
        </div>
        <div id="showHideDiv" style="float: left; height: 850px; width: 5%; background-color: brown;">
        </div>
    </div>
    <div class="div3" style="float: left; width: 89%; ">
        <div style="float: left; background-color: #93D209; width: 100%; height: 50px;display:block">
            hello
        </div>
        <div style="height: 0.5%; width: 100%; background-color: brown;display:block">
            hi
        </div>
    </div>
</form>
</body>
</html>

Answer №1

Check out the Updated Demo

Initially, I've set the CSS for .div3 as follows:

.div3{
    position: absolute;
    width: 89%;
    margin-left: 11%;
}

The margin-left is dependent on the sidebar's total width and positioned absolutely to prevent it from moving down. You can also use 'fixed' positioning if that suits your needs.

When the sidebar switch is clicked, .div3 animates its margin-left to 0% and expands its width to cover 99%-100% of the screen.

 $('.div3').animate({ 'margin-left': '0%', 'width': '99.3%'}, 500);

For toggling back the sidebar, .div3 animates back to its original margin-left of 11% and width of 89%.

 $('.div3').animate({ 'margin-left': '11%', 'width': '89%'}, 500);

Regarding the top switch, I incorporated this jQuery code:

 $("#switch2").click(function () {
        if (!$('#topDiv').hasClass("off")) {
            $('#topDiv').animate({ 'margin-top': '-95%' }, 500);
            $('#topDiv').addClass("off");

        }
        else {
            $('#topDiv').animate({ 'margin-top': '0px' },500);
            $('#topDiv').removeClass("off");
        }
    });

I assigned id attributes (#topDiv and #switch2) to relevant HTML tags like:

<div class="div3">
        <div id="topDiv">
            hello
        </div>
        <div id="switch2">
            hi
        </div>
</div>    

Overall jQuery script:

$("#showHideDiv").click(function () {
            if (!$('#divMenu').hasClass("off")) {
                $('#divMenu').animate({ 'margin-left': '-95%' }, 500);
                $('#divMenu').addClass("off");
                $('.div3').animate({ 'margin-left': '0%', 'width': '99.3%'}, 500);

            }
            else {
                $('#divMenu').animate({ 'margin-left': '0px' }, 500);
                $('#divMenu').removeClass("off");
                $('.div3').animate({ 'margin-left': '11%', 'width': '89%'}, 500);
            }
        });
     $("#switch2").click(function () {
            if (!$('#topDiv').hasClass("off")) {
                $('#topDiv').animate({ 'margin-top': '-95%' }, 500);
                $('#topDiv').addClass("off");

            }
            else {
                $('#topDiv').animate({ 'margin-top': '0px' },500);
                $('#topDiv').removeClass("off");
            }
        });  

Try the Working Sample

Answer №2

 <div class="container">

<div class="menu-btn">
<a href="javascript:void(0);">
    <h6>click me</h6> 
</a>

.container { width:150px; height:500px; background:#000; margin-left:-150px;

}

.menu-btn { width:50px; height:20px; background:#d2d2d2; position: fixed;

z-index: 999;top: 20px;left: 0;margin-left: 25px;}

var flagswap=0;
$(document).ready(function () {
  $('.menu-btn a').click(function()
{
    if(flagswap==0)
    {
        $('.container').animate({marginLeft:0},500);
        $('.menu-btn').animate({marginLeft:'170px'},500);
        flagswap=1;
    }
    else
    {
        $('.menu-btn').animate({marginLeft:'25px'},500);
        $('.container').animate({marginLeft:'-150px'},500);
        flagswap=0;
    }
});  
});

http://jsfiddle.net/uBhR6/

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

Toggle the opening and closing of React components with a map function using onclick

Implementing an onClick function within a map operation, I am encountering an issue where clicking the onClick button changes the state of all items in the map, instead of just the item clicked. This is being done using the useState hook. const [open, se ...

TypeORM ensures that sensitive information, such as passwords, is never returned from the database when retrieving a user

I developed a REST API using NestJs and TypeORM, focusing on my user entity: @Entity('User') export class User extends BaseEntity { @PrimaryGeneratedColumn() public id: number; @Column({ unique: true }) public username: string; publi ...

I am wondering if there is a way to utilize jquery tsort in order to sort an unordered list while having only the ul as

Typically, when utilizing tsort to sort a ul, the syntax is as follows: $('ul>li').tsort(); In my case, I have access to the ul through a variable created like this: var myul=$('#mydiv').find('ul'); My question is, how ...

The submit button is getting disrupted by the background image being set

Implement the following CSS code to set a background image on a single-page app (Angular). .page::before { content: ''; position: absolute; background-size: cover; width: 100%; height: 100%; background-image: url("../../assets/weird. ...

Ways to tidy HTML/XML code?

Upon receiving a web service response, the data appears as such: <text>&lt;span class="TitleServiceChange" &gt;Service Change&lt;/span&gt; &lt;span class="DateStyle"&gt; &amp;nbsp;Poste ...

Utilizing React for handling data exchange between child and parent components

I am still learning about React and material-ui, and I am exploring how to pass data from a child component to a parent component to update the parent state. Currently, when I try to update the state with a new date, it is being logged in the console but t ...

How to arrange data in angular/typescript in either ascending or descending order based on object key

Hey there! I'm fairly new to Angular and have been working on developing a COVID-19 app using Angular. This app consists of two main components - the State component and the District component. The State component displays a table listing all states, ...

EJS include function not working properly

In most cases, my EJS pages include the code below: <%- include('elements/fbviewpagepixel.ejs') %> Everything runs smoothly except for one particular page where I encountered an error message stating include is not a function. I managed t ...

Looking for assistance in adding some animated flair to your website as users scroll

I don't have much experience in animation but I would like to create some animation effects on scroll down. Can anyone provide suggestions? I attempted using SVG paths, but it didn't work out as expected. What I am aiming for is that when a visi ...

Encountering net::ERR_CONNECTION_RESET and experiencing issues with fetching data when attempting to send a post request

I am facing a React.js task that involves sending a POST request to the server. I need to trigger this POST request when a user clicks on the submit button. However, I keep encountering two specific errors: App.js:19 POST http://localhost:3001/ net::ERR_CO ...

Concealing subcategories within a responsive menu using jQuery

My website's responsive menu changes based on screen resolution, but I encountered an issue. On mobile viewports, the sub items in the menu are displayed by default. I want them to only appear when the parent item is clicked. Currently, clicking the p ...

Retrieve all populated fields on the second tier using Node.js and Mongoose

Do you have any insights to share on Node.js and mongoose? I've defined a mongoose schema and when I use findOne(), it returns a document with multiple elements under the "resource" key. Below is an example of how the document looks like: { "met ...

Invoking a function of a Redux-form React component

Is there a way to access the component method of a redux-form? I want my upload button to submit the form and also open the file select dialog if the user hasn't selected any file. Here is my code: UploadModal.js import React from 'react&apos ...

Is there a way to use jQuery to load a ul from a different webpage into a div?

Progress is being made as I work on making two nearly identical pages function seamlessly together. One page features an alphabet list, and when a letter is selected, the other page filters results (last names from a PHP database query) and displays them o ...

Looking for a character that includes both lowercase and uppercase letters

Here is the JSON data I have: [ {"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"}, {"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"} ] var searchBarInput = TextInput.value; var ...

Make sure to attach a resize event handler just once

I am working with a widget that inserts a div element into the DOM. This div requires some JavaScript to handle the resize event effectively. Here's the issue: The widget may be added multiple times on the same page, but I want to avoid adding re ...

Issue: [ng:areq] The parameter 'PieController' is not properly defined, it is currently undefined

Why am I getting an error when using ng-controller='DoughnutCtrl' in a dive? Error: [ng:areq] Argument 'DoughnutCtrl' is not a function, got undefined Here is my Chart.js code: 'use strict'; angular.module('portfoli ...

Creating characters dynamically based on the length of user input in VSCode Snippet

Here is a simple vue-html snippet: { "BANNER1": { "prefix": "banner", "body": ["<!-- ----------------", "/// $1", "--------------------- -->"] } } This sni ...

transferring the value of a textbox into another textbox

I am trying to extract the value from one textbox and transfer it to another textbox. Here is my current code: <input type="text" value="Keyword" id="one" /> <input type="text" value="Search" id="two" /> <a href="#" id="btn">button</ ...

After loading the ajax content, remember to include the JavaScript files

Here's the situation: I am importing some php files, one of which includes a slider that requires .js files. However, when I make an ajax call, the file is imported but the js files are not. Is this normal behavior? I attempted the following: var s ...