演示:http://jsfiddle.net/GmvUQ/5/
更新后的HTML
<div>
<div class="buttons">
<button onclick="changeboleto(0)">Click here</button>
<button onclick="changeboleto(500)">Click here</button>
<button onclick="changeboleto(1000)">Click here</button>
</div>
<div class="circle girl">
</div>
<div class="circle lamborghini">
</div>
</div>
请注意,我已经删除了每个
.circle
中嵌套的
</div>
元素。相反,我为每个元素添加了一个额外的类,设置了
background-image
(如果需要,还设置了一些位置)。
更新后的 CSS
.circle {
width: 250px;
height: 250px;
z-index: 10;
position: absolute;
border-radius: 50%;
overflow: hidden;
margin: 0 auto;
background-repeat: no-repeat;
background-size: cover;
background-origin: content-box;
background-position: center center;
}
.lamborghini {
background-image: url(http://www.hdwallpapers.in/walls/2013_wheelsandmore_lamborghini_aventador-wide.jpg);
}
.girl {
background-image: url(http://www.hdwallpapers.in/walls/colorful_background_girl-normal5.4.jpg);
top: 50%;
}
.buttons {
position: relative;
z-index: 5;
}
我已将大部分CSS移动到.circle
类中,因为它适用于两个图像集。特别注意background-*
属性的值。
更新的JQuery
function changeboleto(pix) {
circleHeight = pix;
circleWidth = pix;
$('.circle').animate({
'width' : circleWidth,
'height': circleHeight
}, 1500, 'linear');
changeCircleBackgroundToWindow();
}
function changeCircleBackgroundToWindow() {
windowWidth = $(window).width();
windowHeight = $(window).height();
$(".circle > div").animate({
'width' : windowWidth,
'height': windowHeight
}, 1500, 'linear');
$(".circle > div").animate({
'width' : windowWidth,
'height': windowHeight
}, 1500, 'linear');
}
我没有混合使用JQuery和CSS过渡效果,而是将所有动画都放在了JQuery中。
我使用了animate()
函数并指定了缓动方法。默认的缓动方法是swing
,但我使用了linear
,因为它可以以恒定的速度进行动画。
编辑
上面的解决方案包括CSS,它允许图像随动画缩放。然而,您要求图像在整个过程中保持相同的“缩放级别”。
要实现这一点,只需从CSS中删除一行,即这一行:
.circle {
...
background-size: cover;
...
}