Adjust the class attribute in real-time

My image doesn't appear in the correct position when I dynamically set it using the margin-top.

Below is an image with a red arrow pointing to the right, which is what I want:

https://i.stack.imgur.com/ue4mY.png

The CSS I have is as follows:

.file_arrow_right {
        background: url('../images/right_icon.png') no-repeat;
        cursor: pointer;
        width: 35px;
        height: 22px;
        background-size: 18px;
        background-position: 0px;
        margin-top: -500px;
    }

I only want to change the margin-top of the above CSS dynamically based on certain image height, so I used the following code:

app.controller('filePreviewSectionCtrl', 
function ($scope, $q, $sce, $rootScope, $filter, $compile, $timeout, $state, 
$modalInstance, commFile, blobUrl) {    
    var myBase64 = "data:"+commFile.fileType+";base64,"+commFile.image+"";
    //jpeg, .jpg, .bmp, .png, .txt
    if(commFile.fileType.split("/")[1] == "jpeg" || 
     commFile.fileType.split("/")[1] == "jpg" ||  
     commFile.fileType.split("/")[1] == "bmp" || 
     commFile.fileType.split("/")[1] == "png") {
        $scope.imgBlob = myBase64;

        var img = new Image();
        img.src = myBase64;
        img.addEventListener('load',function(){
            var width=img.width;
            var height=img.height;
            console.log("width: "+width);
            console.log("height: "+height);
            // should change dynamically
            $scope.arrowMarginTop = "margin-top: "+height/2+"px";
        });     
    } 
})

This is the HTML:

       <script type="text/ng-template" id="filePreviewSectionCtrl.html">
        <div id="dynamicFile" style="width:100%; height:100%;">
            <div class= "file_arrow_right" ng-class="arrowMarginTop">
            </div>     
        </div>          
       </script>

UPDATE:

Following @Temani's suggestion that span elements are inline and won't be affected by margin-top, I changed the span to a div. Then, I used @Satpal's answer to dynamically change the CSS.

Answer №1

If you are defining the CSS rule within the scope variable arrowMarginTop, it is important to utilize the ngStyle directive rather than ngClass

<span class= "file_arrow_right" ng-style="arrowMarginTop">
        </span> 

Make sure to structure it as an object like this:

$scope.arrowMarginTop = { "margin-top" : height/2 + "px" };

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 best way to stretch Font Awesome icons horizontally within a message box and incorporate an image into a uniform circle design using Bootstrap?

I have encountered two issues that I need help with. Firstly, following my tutorial did not result in pictures that are as uniform as the design. Here are the comparison pictures: the first one is from the tutorial and the second one is what I coded. htt ...

Clicking on the initial link will in turn prompt clicks on the subsequent links

Can you please provide instructions on how to correctly simulate a click on the remaining links if a click is made on the first link? jQuery('a.one_num').click(function() { jQuery('a.two_num').click(); jQuery('a.three_num&ap ...

I need to generate table rows using v-for and include a unique value in the 'id' attribute of each row

I am creating table rows dynamically in a view using Flask data. <tr id="<% file.id %>" v-for="file in fileList"> <td><img class="thumbnail_preview" src=""></td> <td><% file.filename %></td> <td> ...

Interactive Navigation Bar in JavaScript

Is there a way to create a horizontal menu with an arrow that follows the mouse cursor? I saw this effect on a website (http://cartubank.ge) and I'm curious if anyone has the source code for it? Your help would be greatly appreciated. ...

Guide to importing a JSON file into Vue.js and HTML

I'm a beginner in Vue and not very familiar with HTML I'm attempting to import data from a JSON file into my interface to display it for the user. Below is the structure of the JSON: [ { "Title": "SOFT-STARTER", "Cod&q ...

Issue with combining overflow-x, Firefox, and JavaScript

In Firefox, an issue arises that does not occur in other browsers. To see the problem in action, please visit this example page: -removed- Try selecting a page other than the home page. The window will scroll to your selection. You can then scroll down u ...

Issues with Access-Control-Allow-Origin in .NET Core Deployment on IIS Set up on Windows Server 2016

I have checked various sources like Microsoft Documentation, SO Answers, and even installed IIS CORS module, but I am still struggling to get it right. The issue arises with my .NET Core API 3.1 hosted on IIS at AWS EC2 Windows Server 2016 datacenter, acc ...

Bootstrap: adaptable card design

I am currently working on replicating a card design similar to this one from WIX using bootstrap. I encountered 2 issues: Initially, the card only touches the contact bar at full screen. However, as I reduce the size of the screen, the card begins to floa ...

Tips on properly styling HTML using the BEM naming convention

I'm trying to implement BEM naming in my project, but I'm having trouble with how to name elements within elements. BEM guidelines state that elements of elements should not exist. How should I approach naming them correctly? <div class="cont ...

Transform socket.on into a promise

Currently, I am developing a service that involves the function init acting as resolve. Initially, it generates a folder using the connection's id and then proceeds to write some default files inside this newly created folder. The main goal here is to ...

Manipulating HTML content with Javascript Array

Is there a way to retain Javascript context from an array? Any suggestions for improving this code? SAMPLE: <html> <script type="text/javascript"> var rand = Math.floor(Math.random() * 6) + 1; var i = Math.floor(Math.random() * 6) + 1; var ...

Attempting to include a standard VAT rate of 5% on the invoice

/* The code format is correct and I don't see any issues on my end, I hope it works well for you. */ I need assistance in adding a fixed VAT (tax) rate of 5%. The tax amount should be displayed on the row, while the total tax should reflect the sum o ...

Guide for converting innerHTML text within an Angular JS controller

Hey! I'm currently working on a Angular JS web application that supports two languages: English and Italian. So far, I've managed to translate all the text from my HTML page like this: {{translation.ERROR_12}} Now, I need to pass some text from ...

Using jQuery or Javascript to enclose every character in a given string with an HTML tag

I am trying to create a function that can take a string of text and wrap each letter within that string with an HTML tag such as <i> or <span>. Although I have made some progress, the current solution is not working as expected. The issue I a ...

Common Error for Novice HTML/CSS Learners

I am currently enrolled in an introductory web design course on Treehouse, and I have encountered a problem with centering a class in HTML using CSS. Despite following the instructions closely, my code is not displaying correctly. Can someone please review ...

Tri-party class switch-up

I successfully implemented two radio buttons to toggle between alternative texts: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

The appearance of online and local websites varies on a single screen and browser

My current dilemma revolves around the difference in appearance of my website when viewed locally versus online. Both versions are connected to the same git repository and use the same css file. Interestingly, I can view the page on both my local machine a ...

Uncovering the jsPlumb link between a pair of identifiers

Could someone help me understand how to disconnect two HTML elements that are connected? I have the IDs of both elements, but I'm not sure how to locate their connection in the jsPlumb instance. Any tips on finding the connection between two IDs? ...

Is there a way to grab code from a different HTML page using JavaScript and jQuery when the user clicks on something?

After testing various codes for the intended purpose mentioned in the title, I have observed a peculiar behavior. The code from settings.html appears briefly and then disappears rapidly. Javascript: function load() { $("#paste").load("settings.html") ...

The Scottish flag isn't displaying on my website, but all other Emoji 5 symbols are working perfectly

When I build my website with PHP, I include header('Content-Type:text/html; charset=utf-8'); at the start. Interestingly, the Scottish flag emoji ...