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

Managing the response from a variable returned by jQuery's .load method

I am facing an issue with my code. I am using jQuery post to validate whether the name of the filter is available for use. $.post('check-username.php', $("#myform").serialize(), function(data) { alert(data); }); When I use the alert to disp ...

Having trouble with the jQuery select2 AJAX functionality?

I've been experimenting with the jQuery select2 plugin and attempting to integrate AJAX with my external data, but unfortunately it's not working as expected. I'm curious if anyone can help me identify what mistake I might be making or if th ...

What is the best method for directing to various pages on a GitHub Pages website?

My GitHub Pages site is live at this link. While the splash page loads perfectly, clicking on the circle to transition to the next page, 'landing.html', leads to a 404 error. I have exhausted all possible solutions to address this issue, includin ...

organize divs in a nested angular style

Looking to showcase div elements in a specific layout within my Angular project: https://i.sstatic.net/RH0lF.png The current HTML structure is as follows: <div class="outer-wrapper"> <div class="image" ng-repeat="image in images" ng-if="sho ...

When scrolling on IOS devices, the fixed element gets hidden underneath the lower element

During the development of my web H5 app, I encountered a technical issue. The fixed element 'Header' is getting covered by a lower element on IOS devices when I drag down the lower element, but it works fine on Android devices. Here is an image ...

Enhance the aesthetics of placeholder text in Semantic UI React using CSS styling

Currently, I am utilizing Semantic UI dropdowns within my React application to generate drop-down menus similar to the ones showcased in their documentation found here: The initial text displayed is "Select Friend", and it appears with a semi-transparent ...

Once you have successfully navigated past Div2, smoothly transition downwards to Div1 and transform it into a sticky

I wish to scroll down, and view #div1... also see #div2... When #div2 disappears from sight, I want #div1 to slide down and stay fixed at the top of the window. If I reach the footer div, I no longer want #div1 to remain sticky. If it's po ...

Unlocking the power of HTML tags in Selenium WebDriver: accessing elements like a pro

I'm working with this HTML snippet. <span class="ng-binding" ng-bind="::result.display">All Sector ETFs</span> <span class="ng-binding" ng-bind="::result.display">China Macro Assets</span> <span class="ng-binding" ng-bind ...

Just diving into JavaScript, what makes jQuery so powerful?

As a newcomer to javascript (although functional programming is no problem for me), I find myself questioning some of the design choices made by jQuery. Is it too late to make changes now, or are they simply too ingrained in the framework? For example, the ...

Creating a 3x3 grid with CSS grid is great, especially when you want to place items in specific columns within the grid

I'm attempting to design a layout that has a unique structure: The blue rectangles are represented by divs. It's like a 3x3 grid, but with spaces in the 3rd and 4th columns of the grid. How can I incorporate these gaps using CSS grid to achieve ...

Pull data from another domain's DIV using jQuery's load/ajax function

I need to load content from a different domain into a DIV on my JSP page. For example: $("#myDiv").load("https://www.google.com") The issue I'm facing is that the request is being blocked by the browser's same origin policy. I've explore ...

Utilize Google Tag Manager to Safely Gather Specific Data

My current project involves capturing values from a <select> element, sending them to the Data Layer, and then forwarding them to an Analytics account via Google Tag Manager. The source code I'm trying to extract values from includes a dynamic s ...

Ways to address time discrepancies when the countdown skips ahead with each button click (or initiate a countdown reset upon each click)

Every time I click my countdown button, the timer runs normally once. But if I keep clicking it multiple times, it starts skipping time. Here are my codes: <input type="submit" value="Countdown" id="countdown" onclick="countdown_init()" /> <div i ...

Implementing pagination within an Angular 11 Mat-table with grouping feature

Encountering an interesting issue with MatTable pagination and grouping simultaneously. I have two components each with a Mat-table featuring Pagination+Grouping. ComponentOne functions smoothly without any issues. When choosing to display 5 elements pe ...

The dropdown menu appears when the user clicks on an empty area

How can I ensure that the drop-down menu is only activated when the arrow or the name is clicked? I've tried looking at similar questions but haven't found a solution. Currently, clicking anywhere next to "shop" activates the drop-down. Here&apo ...

What is the process for submitting a record to a table following the activation of a JavaScript link or button

I am working on creating a like-unlike button below each post for registered users to interact with. I have successfully created the button itself, but now I need to figure out how to store records when a user clicks the button. My idea is to utilize a tab ...

What is the best way to include additional text in a dropdown menu?

I'm trying to add the text "Choose Country" in a drop-down list before the user selects their country. example1 Here is the line of code I used: <option data-hidden="true"> Choose Country </option> However, the phrase does ...

Learn how to incorporate a newly created page URL into a customized version of django-oscar

Just starting out with django-oscar and trying to set up a new view on the page. I've successfully created two pages using django-oscar dashboard, https://ibb.co/cM9r0v and added new buttons in the templates: Lib/site-packages/oscar/templates/osca ...

What is the best way to arrange an array of objects based on a specific attribute?

Ordering object based on value: test": [{ "order_number": 3, }, { "order_number": 1, }] Is there a way to arrange this so that the object with order_number 1 appears first in the array? ...

Triggering an event from a Nativescript application through a webview

I am currently working on transforming my HTML5 website into a Nativescript app. The idea is to enclose the website within a Webview, but I am curious if there is a method for sharing data between the Webview and the Nativescript code. This way, I could tr ...