Create a circular rolling image effect using CSS3 and jQuery when the page is loaded

My div box features a rounded image that I want to roll like a ball on the floor after the page loads. After rolling, I want to position the image perfectly at the center of the box.

css:

#box{width:900px;height:150px;position:relative;background:red;overflow:hidden;}

    .ball{width:150px;height:150px;position;absolute;bottom:0;right:-150px;background:green;}

    .ball img{display:block;width:100%;height:100%;border-radius:50%;}

html:

<div id="box">
       <div class="ball><img src="http://blog.wiziq.com/wp-content/uploads/2013/11/5-150x150.png"</div>
 </div>

Can someone assist me in creating a rolling ball effect using CSS3/jQuery?

My edit:

css:

.ball{
    position:absolute;width:150px;height:150px;right:-150px;
        -moz-transition:all 5s ease;
    -webkit-transition:all 5s ease;
    -o-transition:all 5s ease;
    -ms-transition:all 5s ease;
    transition:all 5s ease;
}

.rotate{-moz-transform:rotate(-940deg);
    -webkit-transform:rotate(-940deg);
    -o-transform:rotate(-940deg);
    -ms-transform:rotate(-940deg);
    transform:rotate(-940deg);}

js:

$('.ball').animate({'right':375}, {duration:500});
$('.ball').addClass('rotate');

Now I need help calculating the precise angle and right position for the ball outside the div box, so the rolling stops perfectly in the middle of the box. Please assist me with this challenge.

Answer №1

Take a look at this fiddle

Styling with CSS:

#container{
    width:800px;
    height:200px;
    position:relative;
    background-color:blue;
    overflow:hidden;
}

.circle{
    width:200px;
    height:200px;
    transition:all 3s ease;
    margin:0 auto;
}

.circle img{
    display:block;
    width:100%;
    height:100%;
    border-radius:50%;
}

Using JQuery:

var startPos = $('.circle').position();
$('.circle').css({'position':'absolute'});
$('.circle').animate({'left':''+startPos.left+'px'});
$('.circle').css({'-moz-transform': 'rotate(360deg)'});

Answer №2

Put your spherical div inside a container.

<div class="sphere-container">
    <div class="sphere">...</div>
</div>

Apply a rotational transition to .sphere and a horizontal transition to .sphere-container. The number of rotations should be calculated as:

(distance traveled)/Math.PI

When applying css3 transitions, I utilized keyframes for positioning.

@-webkit-keyframes{       /*safari and chrome */
    from { left: 270px;} 
    to { left: 1920px; } 
}

@ keyframes{             /* firefox*/
    from { left: 270px;} 
    to { left: 1920px; } 
}

The initial and final positions are defined just like positioning any other element using css.

For guidance on utilizing transitions, visit css tricks. There are helpful resources cited at the end of the article.

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 combine two rows within a single cell using HTML?

Is there a way to create two rows within a single cell? Here is the code snippet that I came up with. I utilized the rowspan attribute to extend the cell width. <tr> <th rowspan="2">Questions</th> <th>sub1</th> <th& ...

jQuery does not allow for text input values to be posted

Having an issue with a form using the POST method. I have some PHP controls that are being calculated in jQuery. While all the form control values are accessible in the next form using POST, the values added through jQuery are not posting to the next form. ...

How have search results traditionally been organized?

I have been dealing with this issue for quite some time now. I am currently working on a page in Django/Python that includes a search function. After searching, users are directed to the results page. However, I am facing an issue: What are the various p ...

Utilizing HTML to call a function and fetching data from AngularJS

I've been struggling to retrieve the value after calling a function in my HTML file. Despite trying various methods after conducting research, I have not achieved success yet. Take a look at the code below: HTML: <div class="form-group"> & ...

Tips on utilizing jquery slideDown alongside appendTo

I am currently utilizing an ajax request to retrieve records from the database and then appending the data in HTML. However, I want the HTML to slide down once the data has been appended, but I'm unsure of how to achieve this. Below is the ajax metho ...

The issue arises when trying to apply a JQUERY css class to a dynamically added div

Utilizing JQuery-1.6.2 and drag-and-drop plugins Below is the code snippet from webform1.aspx: $(document).ready(function() { $( ".draggable" ).draggable({ revert: "invalid" , helper: "clone" }); $( ".droppable" ).droppable({ activeClass ...

Adjusting and arranging multiple thumbnail images within a container of specific width and height

I need a user-friendly method to achieve the following. The thumbnails will not be cropped, but their container will maintain a consistent height and width. The goal is for larger images to scale down responsively within the container, while smaller image ...

Unable to retrieve output from jQuery function in PHP

I'm facing an issue retrieving data from the database and I can't seem to pinpoint why there's no output. My approach involves using jQuery/AJAX: $(document).ready(function(){ $('#banktransfer_blz').blur( function(){ ...

What is the process for converting inline SVG images on the server?

Looking to use inline SVG as a background-image for another element? Check out this example that currently only works in Chrome. If you need an alternative method, take a look at the simpler inline SVG from the RaphaelJS demo here. <svg height="480" ve ...

The website takes forever to load on Internet Explorer and it keeps freezing

When attempting to access a certain website, there is a noticeable delay before the page actually loads (especially when using older versions of Internet Explorer). The issue does not occur with other web browsers. The website in question is a simple Word ...

Issue with making a call to retrieve an image from a different directory in ReactJS

This is how it appears <img className='imgclass' src={"../" + require(aLc.value)} alt='' key={aLc.value} /> I am trying to create a path like ../m/b/image.jpg, where aLc.value contains the path /m/b/image.jpg. I need to add only ...

Organizing flex-items in a specific manner

My goal is to organize flex-items in a specific layout: To achieve this, I am referencing the following example: .Container { display: flex; overflow: hidden; height: 100vh; margin-top: -100px; padding-top: 100px; position: relative; widt ...

Add a message displaying the error in the input field using jQuery Validation

Is there a way to append the jQuery Validation error messages to the input field or get help with positioning them properly? The random popping up of error messages is causing chaos in the form layout. I prefer to have the error messages displayed undern ...

Creating a personalized music player using an audio tag with HTML, CSS, and JavaScript

I am currently working on developing a unique music player from scratch without utilizing the "controls" tag within the audio element. My goal is to build something akin to the SCM Music Player. I've chosen not to use the SCM-provided player due to it ...

Javascript Select Element with No Options

I'm currently working on a <select> element that is being populated from an array using the following code snippet: <select id="selector" name="selector"</select> <button type=button onclick=populateSelect(states)>Click me1!</ ...

Disabling tooltip functionality on an element

Trying to figure out how to remove a tooltip from an element once the text is no longer truncated. The challenge lies in not being able to access the event of the tooltip itself. The tooltip is added by setting an attribute on truncation, but simply removi ...

Tips for Smoothing Out Your jQuery LavaLamp Effect

I'm currently developing a jQuery function to implement the "lavalamp" effect on my navigation bar. Everything seems to be working smoothly, except for one issue. If you hover over an item (e.g., Community Service) and then move your mouse away from ...

The error message "Uncaught ReferenceError: $ is not defined" in Laravel 7.x signifies that

In Laravel 7.x, I have been experimenting with JQuery. I inserted the following code at the end of the body tag in welcome.blade.php to test it out: <script type="text/javascript"> $(document).ready(function () { alert('JQu ...

The Remote_Host index is not visible

We have encountered an issue on our suse11 apache2.2 server when trying to retrieve the REMOTE_HOST information, resulting in an error message stating "index not found" shown below: REMOTE_HOST : Notice: Undefined index: REMOTE_HOST in /media/nss/VOL1/htd ...

Retrieving the background image URL from inline CSS using jQuery

I'm looking for a link similar to url(img/bg/06.jpg), but when using jQuery, I get a longer link like url("file:///D:/WORKING%20FILE/One%20Landing%20Page/version/img/bg/06.jpg") https://i.stack.imgur.com/8WKgv.png Below is the code snippet in questi ...