Animation using jQuery is functional on most browsers, however, it seems to

After creating an animation to simulate an opening door using jQuery, I discovered that it works perfectly on Firefox 24, Chrome 28, and IE 8. However, Safari presents a problem - the door opens but then the "closed" door reappears at the end of the animation to the right of the original door.

Here is the jQuery code:

$('.obrtlcsdoor span a').mouseover(function(){
    $(this).closest('div').children('.obrtlcsdoorimage')
        .stop()
        .animate({
                marginLeft: 55,
                width: 0
            }, 500)
        .height();
});
$('.obrtlcsdoor span a').mouseout(function(){
    $(this).closest('div').children('.obrtlcsdoorimage')
        .stop()
        .animate({
                marginLeft: 0,
                width: 55
            }, 500)
        .height();
});

And here is the HTML code:

<div class="obrtlcsdoor">
    <div class="obrtlcsdoorimage">
        <img src="http://example.com/images/e-door.png" />
    </div>
    <span>
        <a href="http://example.com/">Contact Us</a>
    </span>
</div>

Lastly, the CSS used for styling:

.obrtlcsdoor {
    z-index: 10;
    float: left;
    display: block;
    margin: 10px;
    height: 100px;
    width: 263px;
    background: #ff6900 url('http://example.com/images/e-door-open.png') no-repeat top left;
    overflow: hidden;
    line-height: 100px;
}
.obrtlcsdoor span {
    width: 188px;
    padding: 0 10px 6px;
    font: bold 21px/24px 'Cabin', Arial, sans-serif;
    line-height: 24px;
    display: inline-block;
    vertical-align: middle; 
    text-align: center;
}
.obrtlcsdoor span a {
    display: block;
}
.obrtlcsdoor span a:link,
.obrtlcsdoor span a:visited {
    text-decoration: none;
    color: #fff;
}

.obrtlcsdoor span a:hover {
    text-decoration: none;
    color: #ffba8a;
}
.obrtlcsdoorimage {
    display: block;
    float: left;
    height: 100px;
    width: 55px;
}
.obrtlcsdoorimage img {
    width: 100%;
    height: 100%;
}

If you want to see a live example, check out this JSFiddle link.

I would greatly appreciate any suggestions or ideas to fix the issue with Safari!

Answer №1

My recommendation is to only animate the width of the door and align it to the right.

I believe the animation glitch occurs because jQuery is simultaneously changing the margin and width.

Your slightly modified fiddle: http://jsfiddle.net/F4YkJ/26/

Final HTML :

<div class="obrtlcsdoor">
    <div class="obrtlcsdoorimage">
        <img src="http://outerbridge.co/nonwpitems/e-door.png" />
    </div>
    <a href="http://outerbridge.co/">Contact Us</a>
</div>

CSS :

.obrtlcsdoor {
    z-index: 10;
    display: block;
    margin: 10px;
    height: 100px;
    width: 283px;
    background: #ff6900;
    overflow: hidden;
}
.obrtlcsdoorimage {
    display: inline-block;
    text-align: right;
    float: left;
    height: 100px;
    width: 55px;
    background-color: black;
}

.obrtlcsdoor a {
    display: inline-block;
    margin-left: 55px;
    font: bold 21px/24px 'Cabin', Arial, sans-serif;
    color: #fff;
    text-align: center;
    height: 100px;
    line-height: 100px;
}
.obrtlcsdoor a:link,
.obrtlcsdoor a:visited {
    text-decoration: none;
    color: #fff;
}
.obrtlcsdoor a:hover {
    text-decoration: none;
    color: #ffba8a;
}

JS :

$('.obrtlcsdoor a').mouseover(function(){
    $(this).parent().find('img')
        .stop()
        .animate({
                width: 0,
                height: '100px'
            }, 500);
});
$('.obrtlcsdoor a').mouseout(function(){
    $(this).parent().find('img')
        .stop()
        .animate({
                width: 55,
                height: '100px'
            }, 500);
});

Edit :

I also recommend looking at this pure CSS3 version: http://jsfiddle.net/F4YkJ/30/

It achieves the same result without any javascript, but you may need to consider browser compatibility with CSS3.

Note: The background image of the door was removed because I assumed the door image background itself was solid black, which it is not... I suggest finding the HTML code for the door image background color instead of making another HTTP request.

Answer №2

While the reason for this behavior in Safari is unclear, a workaround is to set the target width to 1 which seems to resolve the issue. If the 1px wide door is causing concern, you could consider hiding it after the animation finishes.

By the way, that's a nice addition to a contact page.

Answer №3

Your jQuery code has been updated. Please review the changes on this JSFiddle link

Additionally, I have included the jQuery snippet below:

$('.obrtlcsdoor span a').mouseover(function(){
   $(this).closest('div').children('.obrtlcsdoorimage')
    .stop()
    .animate({
            marginLeft: 55,
            width: 0
        }, 500)
    .height();
});
$('.obrtlcsdoor span a').mouseout(function(){
   $(this).closest('div').children('.obrtlcsdoorimage')
    .stop()
    .animate({
            marginLeft: 0,
            width: 55
        }, 500)
    .height();
});

I have made the adjustment of replacing .find('img') with .children('.obrtlcsdoorimage')

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

Do not include any null or empty objects when assigning to an array in Mongoose

When using mongoose's find() or findOne() methods, the returned value will be [] and null, respectively, if the where conditions are not met. This can cause issues when assigning these values to an array. Currently, I am filtering out the null values ...

AngularJS makes it easy to retrieve data from a server using the $

I am interested in using Bootstrap datatable with AngularJS. Here is the code I have been working on: https://codepen.io/bafu2203/pen/VzBVmy Upon inspection, you will notice that when attempting to populate the table with data from a $http.get call, the t ...

Navigating URLs to Single Page Application Routing in Vue.js

I'm new to using vue-router and I'm curious if there's a way to manage redirection in Vue or if there are alternative methods in a node.js environment. For instance, when someone tries to access my site by typing the URL example.com/contac ...

Retrieve the URL action and return it as a JSON object within the MVC framework

After my controller method returns a JSON object with a URL formed as shown below: return Json(new { url = Url.Action("ShowContact", "Customer", new { vm = contactVM }) }, JsonRequestBehavior.AllowGet); I am encountering an issue in the success code of m ...

The Proper Usage of Commas in CSS

Check out this CSS code snippet. .form-field {min-height: 20px; margin-bottom: 10px; padding-top: 4px; width: 80px;} .form-field TEXTAREA, INPUT[type='text'] {position: absolute; left: 100px; top: 0px; height: 15px;} .form-field TEXTAREA {height ...

Leveraging a JavaScript variable within a PHP snippet

Similar Question: Sending a PHP string to a JavaScript variable with escaped newlines Retrieving a JavaScript variable from PHP I am trying to work with a Javascript function that accepts one variable, having some PHP code embedded within it. I am ...

Decoding the `this` Mystery in VueJS

Recently, I decided to delve into the world of VueJS starting from square one. Following their official guide has been a helpful resource, but I've hit a roadblock at this section. One particular example in the guide caught my attention... var app5 = ...

How can users create on-click buttons to activate zoom in and zoom out features in a Plotly chart?

I am currently working on an Angular application where I need to implement zoom in and zoom out functionality for a Plotly chart. While the default hoverable mode bar provides this feature, it is not suitable for our specific use case. We require user-cr ...

The Shopify CLI's node serving function has not been operational for a project that is one year old

As I embark on refactoring my previous Shopify project, I encounter an issue when executing the command "shopify node serve" which prompts the following error message. "This command can only be run within node projects." Despite this error message, my pr ...

Employing distinct techniques for a union-typed variable in TypeScript

I'm currently in the process of converting a JavaScript library to TypeScript. One issue I've encountered is with a variable that can be either a boolean or an array. This variable cannot be separated into two different variables because it&apos ...

Dynamically include new events

Here is the code snippet I have in a JS file: $(".dropmenu").on("click", function(event){ event.preventDefault(); $(this).parent().find("ul").slideToggle(); }); On my webpage, I'm using the following code: var append="<ul> ...

What is the best way to retrieve form values on the server in nextJs?

I'm facing an issue with passing form data to my API endpoint in nextJS. I have set up the form using Formik and Yup for validation. Oddly, when I check the console on the client side, I can see the user input values just fine, but on the server side, ...

Animated mosaic pattern menu design

Is there a way to achieve this effect with a sketch? Note: I would like to add hover animation to the borders if possible. I attempted to do it using the following code: clip-path: polygon(0 0, 100% 0, 92% 86%, 6% 100%); However, the shapes created by ...

What are some ways to change the appearance of a component using its parent?

In order to make changes to the CSS properties of a Vue component from its parent, effectively overriding any existing styles within the component, I am seeking a solution. Initially, I assumed that styling a component like <my-component> directly f ...

The app has crashed, currently waiting for any file changes before starting again

Can someone assist me? I am encountering an issue while trying to run npm start. The error pertains to the server.js file. [nodemon] restarting due to changes... [nodemon] starting `node server.js` /Users/johnngo/Desktop/LambdaSchool/HTTP-AJAX/server.js ...

Newbie struggling with executing JavaScript in Visual Studio Code

Hey there! I'm new to coding and have been struggling for the past couple of hours trying to get a simple JavaScript code to run on VSC. Can anyone lend a hand in helping me set up my sandbox correctly? Here's a snapshot for reference: https://i ...

How can you temporarily delay the original ajax request until a certain condition is satisfied before proceeding?

I'm looking to set up a recaptcha process that intercepts all ajax requests before they are executed - here's how I envision the process unfolding: A user performs an action that triggers an ajax request. If the user has already completed the r ...

Error received - CORS request denied on Firefox browser (Ubuntu)

I encountered a CORS error (CORS request rejected: https://localhost:3000/users) while attempting to register a new user. This issue arose from content in the book Building APIs with node.js, Chapter 12. I am currently using Firefox on Ubuntu and have tr ...

Changing date format in Mui DatePicker

I need some assistance with customizing the mui DatePicker. Specifically, I would like to set the date format to display as Day, DD Month. For example: Monday, 10 September Below is the code snippet I am currently working with: <LocalizationProvider d ...

What are some effective methods to completely restrict cursor movement within a contenteditable div, regardless of any text insertion through JavaScript?

Recently, I encountered the following code snippet: document.getElementById("myDiv").addEventListener("keydown", function (e){ if (e.keyCode == 8) { this.innerHTML += "&#10240;".repeat(4); e.preventDefault(); } //moves cursor } ...