The Jquery Function is designed to execute each time the trigger is clicked, however, it is not functioning as

My lovely little website has a charming div that hides any content overflowing the wrapper. I'm trying to adjust the margin-top by $value-120px every time a span with the class of .maisfilme is clicked. Here's my current code:


$(document).ready(function (){
$(".maisfilme").on("click", function() {
     var $mtop = $(this).css('margin-top');

        var $vai = (parseInt($mtop)+ 110 + "px");
    $("#filme").css ({
        'margin-top' : "-"+$vai
    });
});
});

Initially, this code works as intended. It calculates the new margin-top value, adds 120px, and shifts the div upwards. However, the issue arises when clicking on .maisfilme more than once. The function only executes once on subsequent clicks. Can someone please advise me on how to ensure that the function runs each time .maisfilme is clicked?

Answer №1

When you perform the action a second time, the result will not increase in magnitude as desired because of the improper logic used with the sign. Adjusting the sign logic is necessary to achieve a more negative outcome with each iteration.

Furthermore, be mindful that you are retrieving the css from $(this), but applying it to $("#filme"). For the accumulation to occur with each click, these objects must be the same. Ensure they are pointing to the same object for the desired effect.

To achieve a greater negative value with each click, consider implementing either of the following options:

$(document).ready(function (){
    $(".maisfilme").on("click", function() {
        var $mtop = $(this).css('margin-top');

        var $vai = (parseInt($mtop) - 110) + "px";
        $(this).css ({
            'margin-top' : $vai
        });
    });
});

Alternatively, you can use a slightly modified version to handle cases where the initial margin-top value is not zero:

$(document).ready(function (){
    $(".maisfilme").on("click", function() {
        var $mtop = $(this).css('margin-top');

        var $vai = (-(Math.abs(parseInt($mtop)) + 110)) + "px";
        $(this).css ({
            'margin-top' : $vai
        });
    });
});

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

What is the process for integrating the toastr-rails gem into a Rails 6 project?

Hey guys, I've been struggling to integrate the toastr gem into my Rails 6 project where I already have the devise gem set up. My main roadblock seems to be understanding webpacker and how to make toastr-rails webpacker friendly. Despite going throu ...

Rotate text vertically (90 degrees) on IE and Firefox browsers

On one of my pages, I have an asp GridView and I want to display the text vertically to improve its printing quality. I am currently using CSS to achieve this effect: .rotate { -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); width: 25px; ...

jQuery .click() function doesn't seem to be triggering as anticipated

My goal is to create an interactive functionality where: When a user clicks on the h4 element inside a .question divThe .question div expands to a height of 90px, and its child paragraph slides into view with a margin-top of 0If the user clicks the h4 el ...

Error: Unable to locate or read the file "style.scss" for import

I'm currently in the process of setting up a new vuejs project from scratch, but I've encountered an error when trying to import "style.scss" into my component. Specifically, the issue arises in the Login.vue file where I include the style.scss. ...

injecting a substantial number of elements into the DOM

When working on my web application, I am faced with the task of retrieving and displaying a large amount of data in a table format (approximately 800 rows and 10 columns). However, when I attempt to add elements directly using the append() method, the brow ...

Scroll down 1 page using Javascript and Jquery

Hey there! I'm looking to create a website where a small scroll on the mouse wheel will take the site to the next anchor. It's kind of hard to explain, but one good example is the Tesla Model 3 website on the US site here. Does anyone have any t ...

Is there a way for me to determine which class has been selected using jQuery and JavaScript?

I created a list containing various links: <li class="link-1"><a href="#">One</a></li> <li class="link-2"><a href="#">Two</a></li> <li class="link-3"><a href="#">Three</a></li> .. Wh ...

What is the proper way to implement autocomplete in a JavaScript code?

I am attempting to implement autocompletion within JavaScript code, but I'm unsure how to integrate it into a JavaScript table row. Here is where the element is created: Note: $t serves as an incrementing element. $products_specification_ajax ...

spaces between sections with no padding or borders

I came across the following layout on the web and noticed that the divs do not align perfectly. The top of a lower div does not touch the bottom of the div above it when borders are removed, although they do when borders are added. There seems to be an i ...

Navigating through directories in an HTML file

In my directory, the folders are named in uppercase letters and have the following structure: ONLINESHOP |-- index.html |-- COMPONENTS |-- CONFIG |-- TEMPLATES |-- CSS |-- main-style.css |-- CONTROLLERS |-- MODE ...

Ways to invoke the parent function from a child component in JavaScript

Is there a way to execute a parent function within the Child class while preserving the current context (this) of the Child? I attempted using Parent.testFunc.call(this);, but it resulted in an error (Cannot read property 'call' of undefined). ...

What is the best way to make a canvas element fit the entire browser window without causing scroll bars to appear?

My window size isn't working properly, despite attempting the following code: Javascript var game; function game() { this.canvas = document.getElementById('canvas'); this.canvasWidth = window.innerWidth; this.canvasHeight = wi ...

Ensure that all `thead th` elements remain in a fixed/sticky position when using a data

I am facing an issue with my data table requirement. I need to have 2 thead elements in the table, both of which should stick when scrolling. However, only the last thead's tr stays stuck at the top while the first one gets hidden. Can anyone help me ...

Working with Jquery to access and iterate through associative objects

I'm encountering an issue with accessing a JSON encoded PHP associative array. The array is retrieved using $.get(); and stored in the data variable. $.get("includes/ajax/public.php", { do: "get-data", id: value },function(data) { // NO DATA if (!dat ...

Managing varied PHP submissions using a single 'isset' declaration

I'm working on loading data from a MySQL database with PHP and displaying images in HTML. When a specific image is clicked, I want to show text associated with that image using MySQL/PHP. I'm looking for a way to achieve this without having multi ...

Remove multiple HTML rows based on checkbox selection

At first, a table is generated with 5 rows. Now the goal is to remove all rows where the checkbox is checked. This is achieved by adding a checkbox in cell[0] and using a delete button that loops through all rows to delete the selected ones. The process w ...

Move to the first item on the list without any unnecessary scrolling of the view - ReactJS

Whenever I try to scroll to a specific item in the list, clicking on it causes the entire view to scroll instead. Is there a way to ensure that only the div containing the list scrolls? <AlertsList> {productInventoryAlert?.map((prod ...

Customizing CSS for the activity feed on Facebook using HTML5

Can I customize the appearance of the HTML5 Facebook activity plugin with my own CSS? This is the current code for the plugin: <div id="fb-root"></div> <script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d. ...

Child div set to 100% height within parent div with auto height

After scouring the internet for hours in search of an answer, I found myself reading articles like Floats 101 on alistapart and browsing through countless similar questions on stackoverflow. I have reached a point where I feel like I must ask my question b ...

What is the best way to display a loading image and temporarily disable a button for 3 seconds before initiating the process of sending post data from another page via

Is there a way to display a loading image and disable a button for 3 seconds before sending post data from another page using AJAX POST? Once the OK button is clicked, I would like the loading image to appear and the <input type="button" value="Check" ...