2 images . Width resize on both when hover.
Hello. Thanks for reading this. I am new to jQuery and Jscript in general. I wanted to create an effect similar to this :
http://www.sony-asia.com/microsite/mdr-10/#homePage . On the first section you can see that when you hover over one of the 2 images the other fluidly responds and modifies it's width.
So I have tried to do it myself...I thought it was a simple problem but it turned out to be more than that. It works...but not as it should, it has silent errors and it is only working on chrome for some reason. The biggest issue is that even though it works, sometimes the hover event gets stuck..or something and one of the images - the right one to be more precise just...dissapears and comes back again
I have used simple jQuery code to create this effect...more exactly I used the hover function with it's callback and the animate function. The code looks like this.
HTML
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>ACcordion Hover</title>
- <link rel="stylesheet" href="reset.css">
- <link rel="stylesheet" href="styles.css">
-
- </head>
- <body>
-
- <div class="wrapper">
- <div class="item one"></div>
- <div class="item two"></div>
- </div>
-
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
- <script src="script.js"></script>
- </body>
- </html>
CSS
- * {
- margin: 0;
- padding: 0;
- -webkit-box-sizing: border-box;
- -moz-box-sizing: border-box;
- box-sizing: border-box;
- }
-
- .wrapper {
- width: 100vw;
- margin: 0 auto;
- overflow: hidden;
- border: 1px solid grey;
- height:100vh;
- position: relative;
- }
-
- .item {
- height: 100%;
- max-width: 70%;
- background-position: left top;
- transition: width 0.5s ease;
- float: left;
- }
-
- .one {
- width: 40%;
-
- background-color: blue;
- background-repeat: no-repeat;
- background-position: center; center;
-
- }
- .two {
- width: 60%;
-
- background-color: orange;
- background-repeat: no-repeat;
- background-position: center; center;
- }
JS
- $("document").ready(function(){
-
- var $one = $('.one'),
- $two = $('.two');
-
- $one.hover(function(){
- $one.stop().animate({width : '70%'});
- $two.stop().animate({width : '30%'});
- }, function() {
- $one.stop().animate({width : '30%'});
- $two.stop().animate({width : '70%'});
- });
-
- $two.hover(function() {
- $two.stop().animate({width : '70%'});
- $one.stop().animate({width : '30%'});
- }, function() {
- $two.stop().animate({width : '30%'});
- $one.stop().animate({width : '70%'});
- });
-
- });
-
I really need to learn how to think programatically O-o. Can anyone give me some advice ? All the answers I have found implied different images, or complicated scripts, and I know it can be done in an easier way.
Can it be done using css3 only? Thanks.