Adjust the dimensions of an SVG using a dynamic animation

I have a svg image embedded in this object:

<object id="firstrule" type="image/svg+xml" data="/assets/rules/pertinance.svg"></object>

I am looking to create an animation that will make the svg image visible on scroll. I have successfully set up the scroll callback function, but now I need help implementing the animation.

The desired animation effect is to start with the svg image at 0% width, and then have it expand to its full size of 100% when the callback is triggered. The animation should take 3 seconds, gradually increasing the size from 0% to 100% over this duration.

To achieve this, I attempted the following steps: 1) Initially setting the width attribute of the object "firstrule" to 0%

<object id="firstrule" type="image/svg+xml" data="/assets/rules/pertinance.svg" width="0%"></object>

2) Trying to animate the width using jQuery

$('#firstrule').animate({'width':'100%'},3000);

However, this approach did not work as expected, yielding the error:

TypeError: Cannot set property 'cur' of undefined

Do you have an alternative solution (perhaps involving CSS3), or ideas on how to troubleshoot this issue? I am seeking a straightforward resolution without the need for excessive code or plugins. Any insights on overcoming this challenge would be greatly appreciated.

Answer №1

Here's a simple trick you can use: Instead of animating the <object> directly, place it inside a wrapping <div> and animate the container instead.

<style>
    #wrapper { width: 0; }
    #firstrule { width: 100%; height: 100%; }
</style>

<div id="wrapper">
    <object id="firstrule" 
        type="image/svg+xml" 
        data="/assets/rules/pertinance.svg">
    </object>
</div>

<script>
    function animate() {
        $("#wrapper").animate({ width: "100%" }, 3000);
    }
</script>


Check out this JSFiddle example

Answer №2

If you're looking to engage directly with the object, javascript is your trusty companion:

var i = 0;

var repeat

function move() {


    repeat = setInterval(function () {
        adjust();
    }, 1);

    function adjust() {
        document.getElementById("firstrule").style.width = i + '%';
        document.getElementById("firstrule").style.height = i + '%';
        i++;
        if (i >= 100) {
            clearInterval(repeat);
        }
    }


}

JSFIDDLE

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

Create dynamic text on an HTML canvas starting from the center, displaying one character at a time while constantly adapting to the formatting

I am currently in the process of developing a website that has the capability to animate text, drawing each character onto a canvas one by one. I have successfully completed the initial coding for this functionality. However, my next challenge involves cen ...

Utilizing Async / Await in the created lifecycle hook - Vue2

I recently installed the vue-element-loading package and added its component to my page.vue: <vue-element-loading :active="isActive" :is-full-screen="true"/> After adding a variable to my data: data () { return { isActive: false, } } I th ...

Is it possible to delete a <div> tag based on screen size using jQuery or JavaScript?

Hello, I was curious if it's possible to dynamically remove a specific div tag using jQuery or JavaScript when the screen reaches a certain size, for example 500px. ...

`Take action on a row by selecting the Edit option in CodeIgniter`

In my table, I have data displayed with an edit button in each row. What I want is for a pop-up or lightbox to appear when the edit button is clicked, without refreshing the page, and display all fields within that box. I am familiar with updating in the c ...

Using Node.js to handle reading files and dealing with undefined or null values

The get method is responsible for receiving a userid with an initial total number of points defined in the stcok.json file, along with various transactions stored in another file. Below are some sample entries from the stock JSON: [ { "user" ...

Could someone please assist me in improving my comprehension of this tutorial?

I am currently working my way through a tutorial I found on this website. The tutorial appears to cover everything I need, but I'm struggling to grasp some of the concepts. It could be because I don't have enough knowledge about Xval and jquery. ...

Changing the behavior of one div when the mouse hovers over another div

My coding challenge: HTML: <div class="example">example</div> <div class="sample">sample</div> <div class="demo">demo</div> CSS: .hidden { display: none; } Javascript: $(document).ready(function(){ $(".example ...

Updated navigation bar and optimized for mobile devices

I'm struggling to get my navigation menu to stick at the top and be responsive on mobile with a toggle bar icon. I'm using Bootstrap 4, and I've tried using sticky-top and fixed-top in the div classes, as well as position fixed in my header ...

What is the best way to reset Owl Carousel following an ajax request?

I am attempting to reinitialize the owl carousel following a successful ajax call. The ajax call will update the data while retaining the same view. I am encountering an issue where the carousel structure in the view does not reinitialize, and I am unsure ...

Angular 2's Component background color styling

Within my Home component, there is a carousel component. I want the background color of the home section to be Gray and the background color of the carousel to be blue. Unfortunately, I am having difficulty setting a background color for the body tag of t ...

Is it not possible to update the innerHTML stored in a variable in real-time?

I am attempting to link a div's text with a variable that changes its value based on different button clicks (using plain JavaScript), but I am struggling to update the inner HTML dynamically. It only displays the initial value set on page load. What ...

Could someone assist me in figuring out the reason behind my fetch method sending undefined values?

Looking for some assistance with my fetch implementation issue. I am currently working on integrating stripe into my online grocery store website utilizing a node.js server with express, and ejs for frontend integration. The client-side JavaScript uses a f ...

What is the best way to center a CSS box on the screen?

I am attempting to center the login box on the screen, but when using position: absolute the background color changes to white. /* all */ .patua_font { font-family: 'Patua One', cursive; } .Roboto_font { font-family: 'Roboto&apo ...

Firefox and Internet Explorer do not support CSS transitions

I'm trying to achieve a simple style with the following code: li, a { display: inline-block; } li { transition: top 0.3s; } li:hover { position: relative; top: 3px; } This style is intended to make some icons in a menu sink down ...

Font reference in IE and Chrome causing HTTPS to fail

Take a look at in both Internet Explorer and Chrome. The header tags (h1 through h6) all have font specifications that rely on a javascript fonts.com specification. However, there seems to be an issue with rendering properly. Interestingly, the fonts ar ...

Transform the dynamic JSON format into a key-value pair structure with nested children nodes

Looking to convert a dynamic JSON structure into a tree node: { "video": { "width": 1920, "height": 1080, "video_codec": "H264", "CBR": "4337025", "frame_rate& ...

Relocate the resizable handles in jQuery outside of the div elements

I currently have 3 nested divs. Using jQuery $(function() { $("#div1").resizable({ handles: "n, e, s, w, nw, ne, sw,se" }); $("#div1").draggable(); }); Within the HTML structure <div id="div1" style="left: ...

Tips for ensuring that the dropdown is always visible in v-autocomplete/v-select

I'm currently working on a project utilizing the v-autocomplete component, which is actually an extension of the v-select element. In my scenario, I need to place this select element inside a v-menu container. When attempting to wrap a v-select with ...

Angular: Modify value when checkbox is ticked

Controller: promiseObj.physical = $http.get('search?sumBoth=1&customer=' + customer).success(function(data) { $scope.servers = data; // retrieve data from JSON }); View: <tr ng-repeat="data in servers | filter: { _id: { idc: item._ ...

Creating a full-width layout with CSS div

Seeking the most stylish method to create a layout similar to this using divs in IE7 and other modern browsers (Chrome, Firefox, etc). Appreciate your help! ...