Locking element in place beneath precise browser width

Below is the script I am working with:

$(window).resize(function(){
if ($(window).width() >= 992){  

    var windw = this;

$.fn.followTo = function ( pos ) {
var $this = this,
    $window = $(windw);

$window.scroll(function(e){
    if ($window.scrollTop() > pos){
        $this.css({
            position: 'static',
            top: pos,
            marginTop: 100


        });
    }
    else {
        $this.css({
            position: 'fixed',
            top: 100
        });
    }

});
};


 $('#bgForm').followTo(1550);    



}   
});

I am looking to prevent the position from being fixed when the browser size is below 992px. Is there a way to achieve this using my current script? I have attempted with the provided code, but the element gets stuck in position. Thanks in advance!

Answer №1

It seems like you could achieve your goal with a CSS media query instead of using Javascript. Is there something specific you haven't mentioned in your question?

@media only screen and (max-width: 992px) {
    // These rules will only take effect when the 
    // browser window is less than 992px wide
}

Answer №2

According to @designedbyscott, implementing a media query seems like the most straightforward solution. However, in relation to your JS script inquiry, here is a streamlined approach. This version involves creating a function that assesses the window width and adjusts the position accordingly. The function is triggered on load, resize, and scroll events, though the final decision on event triggers is at your discretion. Simply replace 'your-element' with the specific element you wish to target.

function positionAdjust() {
  const element = $('.your-element');
  const width = $(window).width();
  const scroll = $(window).scrollTop();
  if (width >= 992 || scroll > 1550) {
    element.css('position','static');
    element.css('margin-top','100px');
  } else {
    element.css('position','fixed');
    element.css('top','100px');  
    element.css('margin-top','0');  
  }
}
window.addEventListener("load", positionAdjust);
window.addEventListener("resize", positionAdjust);
window.addEventListener("scroll", positionAdjust);

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

CSS - spacing anomaly among inline-block items

I am facing a strange issue with the arrangement of elements in my container div. I have set them to display: inline-block, and they are supposed to expand horizontally if the contents exceed the device width. However, there is an unexpected margin between ...

Having trouble getting Jquery to work with an Ajax call in the cart drawer. Any suggestions on how to resolve this issue

I used this code to implement a collapsible feature in my Cart Drawer, but it seems to stop functioning after an Ajax call. Any suggestions on how to troubleshoot and fix this issue? Just for context, I included onclick="return false;" in the bu ...

The code `$(body).css({'background-image':'url'}) is not functioning properly

Is it safe to assume that the .css() method cannot be applied to the body tag? Since it seems to work fine when used on $("#div")... ...

Is anyone else experiencing differences in the appearance of my mega menu between Safari, Firefox, and Chrome? It seems like

When viewing in Safari, the text overlaps for some reason. It works fine on Firefox and Chrome. Here's a screenshot of the issue: Safari: https://i.stack.imgur.com/hf7v2.png Firefox: https://i.stack.imgur.com/NrOOe.png Please use Safari to test th ...

Determine the aspect ratio of the image and incorporate it into the query string

In my current code, I am extracting the dimensions of an image obtained from a cropper tool. I then calculate the aspect ratio of the image by dividing the height by the width. This value is then incorporated into the query string url: urlLocation + imag ...

How can jQuery be used to split a string and isolate the desired string in the center? For example, if we have a string like: sample_str[targeted_str][other_str

http://jsfiddle.net/150aby4u/ There are several dropdown menus, each with a unique field name that is incremented. The objective is to extract the number in the middle of the string/text after making changes to the dropdown. In this case, we want to retr ...

Stop flexbox list items from altering the width of their parent container when the children are hovered over

In my project, I am dealing with a series of list items where some have child lists containing sub navigation links. To create a hover effect using CSS, I set display: none on the child <ul> and then change it to display: block when hovering over the ...

What steps do I need to take in order to implement a basic ZeroClipboard copy-to-clipboard feature in jQuery on jsFiddle with just one click?

I'm having trouble implementing ZeroClipboard in a jQuery environment. My goal is to have the text within each div with the class copy copied when clicked. The following jsFiddle demonstrates the functionality with double click using the stable ZeroC ...

Exploring the functionalities of jQuery and Local Storage: Understanding the Selector's various states

I am currently developing a new feature for font customization on a website. The drop-down menu offers several font options to choose from. When a user clicks on one of these options, the following code is executed: jQuery(function($) { if (localStorage.g ...

What is the process for customizing the color of the focused label in Material UI through styling?

Recently, I delved into the world of JSS for styling and ran into an intriguing issue. My goal was to modify the color of a label in an InputLabel component when it is in focus state. After some tinkering, I managed to achieve this using the code snippet b ...

We are hosting an event focused on DOM text selection outside of Input or TextArea elements

I need help finding a Javascript event that triggers when a user highlights paragraph text with their mouse on a web page. Once the text is highlighted, I want to access it using window.getSelection(). Just to clarify, I am not looking for ways to capture ...

Having trouble passing a JavaScript variable through AJAX requests

In this particular script, I am encountering an issue where the variable "encrypted" is expected to be sent via ajax to upload.php, however I am unable to achieve this. Oddly enough, if I substitute the "encrypted" variable with another variable in the a ...

Show the button's value on the text box using JavaScript

When using bootstrap, I encountered an issue where the value of a button would display in a textbox upon clicking it, but then quickly disappear. This unexpected behavior left the textbox empty prematurely. <input type="submit" value="5000t "class="btn ...

What is the best way to retrieve the checkbox value using AJAX/jQuery with a Spring form?

My form contains a group of checkboxes identified by the path deliveryStatus, like so: <form:checkbox path="deliveryStatus" value="notDelivered"/> <form:checkbox path="deliveryStatus" value="delivered"/> I came across two helpful examples: E ...

Ways to identify when a person has scrolled a specific distance

Is it possible to modify the appearance of my top navigation bar once the page has been scrolled a specific number of pixels? I assume this requires some sort of listener to track the amount of scrolling. I came across element.scrollTop, but it doesn' ...

Deactivate the child division by the nth parent division

At the end of my question, I have included a dynamically created div structure with the id "forth-three-one". Now, I need to find a way to disable or hide the input inside this specific div. Directly using the id "forth-three-one" is not an option as it w ...

Choose the option for overseeing safaris

Hello there! I need some help with Safari. Can you please guide me on how to disable the arrows? https://i.stack.imgur.com/1gzat.png ...

Transforming an image by masking it to create a binocular-like view

I've been experimenting with creating a unique visual effect where an image is revealed through a binocular-shaped element that follows the mouse cursor. The circular part of the element moves along with the cursor, unveiling parts of the image as it ...

How can I use jQuery to display a larger image in a specific div when a thumbnail is clicked?

My current setup involves a div that serves as a target: <div id="rightbox"></div> I have my thumbnail images neatly organized into groups: <img src="group1/thumb/01.png" width="160" height="97" class="imgtopleft" /> <img src="group ...

Elastic canvas to always match the full size of the container or screen

I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio. The problem arises when th ...