背景图像缩放效果。

时间:2015-03-10 08:02:29

标签: javascript jquery html css

这是我的代码

的HTML

<h1 class="logo-title"><span>DAVE</span></h1>

的CSS

h1.logo-title {background: url(bg.jpg) no-repeat;background-position: 50% 100%;-webkit-background-clip: text;}

我正在尝试使用我的背景图像zoomin和缩小动画效果,就像那些网站一样。这是演示。 http://wowslider.com/jquery-slider-bar-kenburns-demo.html http://owwwlab.com/toranj/index-2.html

请为我的背景图片提供任何溶剂。有人在这种情况下帮助我。 ??

3 个答案:

答案 0 :(得分:0)

你可以在这里得到这个想法:

<强> HTML:

<div class="zoom-in strawberry">
</div>
<button onclick="zoomin(100, 2, 500)">Zoom In</button>

<强> CSS:

.strawberry{
    background-image: url('http://dreamatico.com/data_images/strawberry/strawberry-6.jpg');
    background-repeat: no-repeat;
}

.zoom-in{
    display: block;
    width: 400px;
    height:350px;
    background-size: 400px;
}

<强>脚本:

function zoomin(interval, sizeChangeEveryInterval, stopResizeWhenSizeReached){
    var timeout = setInterval(function(){
        var target = $(".zoom-in")
        var bgposx = target.css('background-position-x') ? parseInt(target.css('background-position-x')) : 0;
        target.css('background-position-x',(bgposx - sizeChangeEveryInterval) + "px");
        var bgposy = target.css('background-position-y') ? parseInt(target.css('background-position-y')) : 0;
        target.css('background-position-y',(bgposy - sizeChangeEveryInterval) + "px");
        var bgsize = !isNaN(parseInt(target.css('background-size'))) ? parseInt(target.css('background-size')) : 100;
        target.css('background-size',(bgsize + sizeChangeEveryInterval) + "px");
        if(stopResizeWhenSizeReached < bgsize) clearInterval(timeout)
    }, interval)
}

这是我的小提琴:http://jsfiddle.net/ThisIsMarkSantiago/xm1zp09g/

答案 1 :(得分:0)

最简单的方法是在您的内容后面添加一个img标记,然后transform: scale(110%,110%);。使用css对其进行动画处理:

div.container
{
    position: relative;
}
img.background
{
    position: absolute;
    z-index: -1;
    left: 0px;
    // .. etc etc
    transform: scale(100%, 100%);
    transform-origin: 50% 50%; // wherever you want the 'midpoint' to be at.
    transition: transform 5s linear;
}
img.background.active
{
    transform: scale(110%, 110%);
}

容易做到。

你当然可以只用background-image来做这件事,但是你会有更艰难的时间从图像切换到图像。

答案 2 :(得分:0)

您也可以使用纯CSS3动画解决此问题。虽然它是might not be supported on every platform

这是一个working example(很快被黑客攻击并在Chrome中测试过):

&#13;
&#13;
html, body {
    min-width: 400px;
    min-height: 300px;
    height: 100%;
    width: 100%;
    padding: 0;
    margin: 0;
}
.container {
    position: relative;
    height: 100%;
    width: 100%;
    background: #efefef;
    overflow: hidden;
}

.image {
    background-size: cover;
    background-repeat: no-repeat;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    -webkit-animation-name: zoom;
    animation-name: zoom;
    -webkit-animation-duration: 10s;
    animation-duration: 10s;
    -webkit-animation-direction: normal;
    animation-direction: normal;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}

.image-1{
    background-image: url(http://www.chip.de/ii/917741674_393fa282fa.jpeg);
}
.image-2{
    -webkit-animation-delay: 5s;
    animation-delay: 5s;
    background-image: url(http://www.chip.de/ii/917741714_fe3259c85e.jpeg);
}

@keyframes zoom {
   0% {transform: scale(1,1); z-index: 2;}
   49% {transform: scale(2,2);}
    50% {opacity: 0}
}


@-webkit-keyframes zoom {
   0% {-webkit-transform: scale(1,1); z-index: 2;}
    49% {-webkit-transform: scale(2,2);}
    50% {opacity: 0}
}
&#13;
<div class="container">
    <div class="image image-1" style=""></div>
    <div class="image image-2" style=""></div>
</div>
&#13;
&#13;
&#13;