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

Tips for distinguishing individual submit buttons within a foreach loop

Here is my code snippet: <?php require_once 'core/init.php'; if($result = $db->query ("SELECT * from countries")) { if($count = $result->num_rows) { $rows = $result->fetch_all(MYSQLI_ASSOC); } ...

Utilizing Vue.js 2 to dynamically update data through directives

Is it possible to change a data property from a directive using single file components? For instance, consider the following code... export default { name: 'app', data: function() { return { is_loading: true ...

Having trouble updating text in a PDF using NodeJS and hummus library

How can NodeJS be used to replace a string in a PDF file? offers a solution for text replacement in a PDF document. However, when applying the same code, an issue arises where the replaced text is visible in the source code of the PDF but does not render p ...

update url to redirect hashbang with history.js

Upon further investigation, I have observed that history.js has the ability to automatically transform http://www.site.com/some/path#/other/path into http://www.site.com/other/path when used with a html5 enabled browser. While this feature is useful, ...

Setting line chart data for Chart.js

Can you help me troubleshoot an issue I'm facing with creating a dataset for a line chart in Chart.js? Despite having an array of objects, the dataset isn't rendering correctly and I end up with two line charts instead of one. What could be causi ...

Retrieving, storing, and implementing the classes of child elements directly from an array using jQuery

With the help of jQuery, you can retrieve child elements of the #parent element that have the class .west. In this case, we are not interested in fetching the content of these child elements but instead their class names. For example: <div id="parent" ...

Exploring the iteration of objects utilizing underscore.js

Currently, I am diving into the world of backbone.js and encountering a slight issue while iterating over some models in a view. The first code snippet seems to be functioning correctly, but the second one, which is underscore.js-based, does not work as ex ...

CSS will only be visible if it is enclosed within the <style> tag

While creating a view, I noticed that my CSS is not being displayed unless it's placed inside a <style> tag. I've tried using !important without success, as well as utilizing the selector .container-fluid > .row > .d-flex > .out_pr ...

$scope.apply is triggering both "catch" and "then" handlers

I am trying to ensure that the content of a page in Angular 1.6.2 and UI router is only displayed once it has been confirmed on the server that the user has the appropriate role/permissions. It seems like without using $scope.apply(), the catch() function ...

The error message displayed reads as follows: "topojson has encountered a TypeError: Unable to access the property 'feature' as

Context A problem has arisen with JavaScript when trying to use the topojson.feature(topology, object) function. It seems that this issue appeared after moving from TopoJSON version 1.6.26 to version 2.x, although the functionality remains similar. The p ...

Simple JavaScript validation for inputting names

Hey there, I'm a beginner in javascript and I am struggling with a simple input validation using else if statements. Every time I run the code, it goes directly to the else condition. Can someone please assist me? <!DOCTYPE html> <html lang=& ...

Creating dynamic dropdown menus using JSON files in jQuery mobile is a useful technique for enhancing user experience on

I am working with a massive table (8 MBytes) that I need to filter using a small JavaScript application. The process works as follows: Countries Regions Skills I want the user to select one country, one region, and multiple skills as filters. Based on ...

How can I transform an ajax request into unobtrusive javascript?

Here is the ajax code I have: $('select#books_order').on('change', function() { $.ajax({ url: "/books/#{@book.id}", type: 'GET', dataType: 'script', data: { sort: $('#books_order option:s ...

invoke a function upon successful completion of an ajax call in a datatable

Can we trigger a JavaScript function after a successful AJAX call in a datatable? Here is the code I am attempting to use: var dataTable = $('#app-config').dataTable( { "bAutoWidth": false, ...

Exploring the power of ElasticSearch alongside Mysql

As I plan the development of my next app, I am faced with the decision between using NoSQL or a Relational Database. This app will be built using ReactJS and ExpressJS. The data structure includes relational elements like videos with tags and users who li ...

Vue.js blocks the use of iframes

I've come across a peculiar issue where I need to embed an iframe inside a Vue template and then be able to modify that iframe later. The code snippet below shows the simplified version of the problem: <html> <body> <div id="app" ...

Guide for bringing in a complete npm library into React Native beyond just a single file access

Currently, I am facing an issue with importing the sjcl library into my project. Even though there are multiple files within the library, I am only able to import one file successfully. This is what I have tried from the command line: npm install sjcl -- ...

Is there a concept in JavaScript that includes a data structure called a "matrix" with elements arranged in a grid-like format accessed by

I'm not familiar with the terminology, but I'd like to simplify it: var topicList1 =['hello','hallo', ..., 'hej']; var topicList2 =['a','b',...,'c']; ... var topicList999 =['x,&apo ...

Consolidate HTML project into a unified HTML file

In my current project, I am working with a static HTML report that is organized in the structure outlined below: 1.css 2.font 3.js 4.pages 5.attachments 6.index.html I would like to know how I can combine all these elements to create a unified 'repor ...

Changing the border of an iframe element using jQuery or Javascript from within the iframe itself

Is it possible to set the border of an iframe to zero from within the iframe itself using JavaScript or jQuery? Any guidance on how this can be achieved would be greatly appreciated. Thank you! ...