Is it possible for a div element to maintain a fixed position even as its size changes

Looking to adjust the size of a ruler?

In horizontal mode, the ruler expands and contracts as you resize it.
However, in vertical mode, the ruler shifts left as you shrink it and right as you expand it.

You can resize the ruler by dragging the small red line.


Check out the code on jsFiddle.

Snippet from the source:

ruler.css({
    width: e.pageY -ruler.offset().top
});

Full source code:

$.event.add( document, "mousemove", function(e){
e.stopPropagation(e);
if(ruler_resize_active) {
    console.log(".ruler-resize:mousemove");
    console.log(ruler.offset().left);
    console.log(ruler.offset().top);
    if(options.orientation == "vertical") {
            ruler.css({
             width: e.pageY - ruler.offset().top
        });
            }
     }
});

Answer №1

Unfortunately, I am not well-versed in jQuery, so I am unable to provide you with the most efficient method for implementing the fix. However, based on my understanding of your issue, it seems that adjusting the transform-origin should resolve the problem. By changing the default center-center setting to something like top-left, the resizing behavior should align with your expectations.

To demonstrate, I modified the CSS code within the ruler element to include a webKit prefix:

var ruler = $(this)
    .addClass('ruler')
    .css({
        minWidth: options.minWidth,
        maxWidth: options.maxWidth,
        width: options.width,
        webkitTransformOrigin: "top left"
    });

You should insert this adjustment at the appropriate location where jQuery typically handles such transformations. Ensure that you account for other browser prefixes and remember to include the unprefixed version as transformOrigin. This change is likely best placed before applying any rotations or additional parameters.

For a visual example, refer to this JSFiddle link.

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

Is it common for Google Chrome console snapshots to have a prolonged idle time?

I noticed in Google Chrome console snapshot that my JavaScript code has a long "Idle" time. I'm not sure if this is normal. Attached is a screenshot for reference: https://i.stack.imgur.com/k0mkp.png Could this extended "Idle" time be attributed to ...

Assign the value/text of a div element by using JavaScript/jQuery within a PHP loop

I'm struggling to figure out how to set the value/text of a div using javascript/jquery inside a loop. Can anyone offer some guidance on this issue? Goals: Extract data from a database. Use javascript/jquery to assign the extracted data to an eleme ...

Declaring a Javascript variable within an if statement does not alter the value of the global variable

Currently, I am working on enhancing my HTML projects by using JavaScript to modify the video source. Below is the video element in question. <div> <video id="songVid" onmouseover="controlsVid()"> <source src=&qu ...

Personalize your jQuery stack chart

I want to create a bar chart that plots users against work performance. I have been looking into using canvasjs.com to implement it as a stack chart, like shown However, I need to divide each section based on time. I am aiming for the chart to look simila ...

What is the best way to display JSON data in a Listview control?

What is the best way to display JSON data in a list format? Currently, I am able to retrieve JSON data and display it in an alert dialog. The JSON data looks like this: [{"_id":"5449f20d88da65bb79a006e1","name":"name3","phone":"888888","service":"service ...

Impact of designs on the elements within components

Here's a CSS query I have: If I have the code below, does the styling apply to the contents of v-container but not v-container itself? <v-app id="inspire"> <v-container justify-center style="max-width: 1200px"> <v-layout row ...

Alerting JavaScript with element Selector doesn't function at full capacity

I am currently implementing Notify JS from the following source : Below is the HTML code I am using: <div> <p><span class="elem-demo">aaaa</span></p> <script> $(".elem-demo").notify( "Hello Box" ...

Trouble with loading Google chart data from a local JSON file

The Issue Running multiple charts on a site simultaneously is causing page blocking while loading all the data before rendering the charts. The goal is to trigger the render function for each chart as soon as the data is available, rather than waiting for ...

The ng-include feature seems to be malfunctioning

After building a basic web page using AngularJs, I ran into an issue when attempting to integrate header.htm into index.html - nothing was displaying in the browser. index.html <html> <script src="https://ajax.googleapis.com/ajax/libs/angul ...

The jQuery toggleClass() function is not being applied successfully to content that is dynamically generated from

I've created an awesome collection of CSS-generated cards containing icons and text with a cool animation that expands upon tapping to reveal more icons and options. I carefully crafted the list to look and behave exactly how I wanted it to. But now, ...

Should I assign a variable to $(this).data("whatever") or not?

Does jQuery data() involve any CPU performance in retrieving values? Here's the scenario: I have multiple links/buttons like the one below <a href='#' class='ui-btn' data-counter='1234'>test</a> When a link ...

Divide a SINGLE BACKGROUND IMAGE in HTML into two separate links of equal size, one at the top and

As a beginner in HTML, I am trying to find a way to divide a background image into two equal sections without using image mapping. I attempted to split the links by setting the style to 0% and 50% to designate the top and bottom halves, but unfortunately, ...

Collapsing or expanding the Material UI accordion may lead to flickering on the page

import React from "react"; import { makeStyles } from "@material-ui/core/styles"; import Accordion from "@material-ui/core/Accordion"; import AccordionDetails from "@material-ui/core/AccordionDetails"; import Accordi ...

What steps should I take to make the image appear on the browser?

I am having trouble displaying an image on a webpage in the browser. Despite using the correct path to the image, only the alt tag is being shown along with an ordered list below it. Strangely, Visual Studio Code suggests files while building the path in t ...

Linking to a specific div within a Vue.js component

Currently working on a Vue.js project, I am encountering an issue with linking an anchor to a specific div within a component. The anchor in question is as follows: <a href="#porto" class="porto-button">Porto, Portugal</a> Accompanied by the ...

Can I position two div elements next to each other, with one div partially offscreen, without utilizing display none or setting its width to zero?

Is it possible to position one div outside of the screen width while setting two divs side by side? I am looking to insert DIV2 above DIV1 with animation without adjusting its width or display. The goal is for DIV2 to enter the screen from the side alread ...

Positioning CSS for a Responsive Button

Currently, I am working on making my modal responsive. However, I am encountering an issue with the positioning of the "SAVE" button. The button is not staying in the desired position but instead disappears. Below is the snippet of my HTML and CSS: .dele ...

Customize the jQuery Datepicker to show the date in a format without the year, while utilizing

I am exploring how to utilize the jQuery datepicker tool to display only the Day, Month, and Date to the user, similar to this: Fri. Aug. 24 However, in the background, I need to keep track of the year. To achieve this, I attempted to use the altField ...

What is the most efficient way to define the font properties for all H1-H6 headings in a single declaration

Is there a way to set all font properties for H1-H6 headings in one CSS declaration? CSSLint.net is flagging multiple definitions of heading styles as an issue. I currently have specific styles for each heading level: h1 { font-size: 1.8em; margin-top: ...

Effortless AJAX/jQuery solution for displaying live file updates while copying

As I delve into the world of Ajax/jQuery, my current focus is on setting up a test environment for the following scenario: The server needs to recursively copy an entire folder from one location to another. The client should then display the list of copi ...