使用 CSS 或 JavaScript 缩放背景图片
Zoom Background Image with CSS or JavaScript
我想随时间缩放我的 div 的背景图片,这样它看起来就像在放大和缩小(无需将鼠标悬停在上面 - 我只是在寻找动画自动启动)。但是,无论我尝试什么,背景动画看起来都是断断续续的。
如果我将 div 设置为缩放,那么过渡是平滑的,但它会缩放整个 div,而不仅仅是背景图像。有关更多信息,我是 运行 最新版本的 Chrome,并且已经尝试过 Windows 和 OS。你可以在这里看到一个例子:https://jsfiddle.net/sdqv19a0/
<div class="site-main-banner">
<div class="caption">
<!-- Bootstrap -->
<div class="container">
<div class="row">
<div class="col-xs-12">
<!-- figure -->
<figure> <img src="images/signature.png" alt="signature"> </figure>
<!-- H1 Heading -->
<h1 class="typewrite" data-period="2000" data-type='[ "Purposeful design", "Data-driven marketing", "Dynamic branding", "I love it all." ]'> <span class="wrap"></span> </h1>
<!-- H2 Heading -->
<h2>graphic designer • web designer • analytical marketer</h2>
<div class="clearfix"> </div>
<!-- Button -->
<a href="#" class="theme-btn">view my portfolio</a>
</div>
</div>
</div>
</div>
CSS:
.site-main-banner {
float:left;
width:100%;
background:url(https://preview.ibb.co/m8kxST/static_banner_new.jpg) no-repeat center center;
background-attachment:fixed;
background-size:cover;
margin: 0;
padding: 0;
display: block;
clear: both;
position: relative;
text-align:center;
overflow: hidden;
animation: shrink 5s infinite alternate steps(60);
}
@keyframes shrink {
0% {
background-size: 110% 110%;
}
100% {
background-size: 100% 100%;
}
}
.site-main-banner a.theme-btn {
color: #FFFFFF;
border:#FFFFFF solid 1px;
background:none;
box-shadow:none;
}
.site-main-banner a.theme-btn:hover {
color: #bc6ca7;
border:#FFFFFF solid 1px;
background:#FFFFFF;
box-shadow:none;
}
.site-main-banner .caption {
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%);
padding:300px 0;
}
.site-main-banner figure {
float:left;
width:100%;
text-align:center;
padding:0 0 15px 0;
}
建议?我也对 js 解决方案持开放态度,只是对 javascript 不太满意。谢谢!
我会将 css transform
属性 与伪元素结合使用。
.site-main-banner {
position: relative;
overflow: hidden;
}
.site-main-banner:after {
content: “”;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: -1;
background-image: url(...);
// put your animation here
}
我无法真正向您发送一个完整的示例,因为我目前正在地铁 phone,但我希望您能理解。
尝试在动画中使用 transform: scale([values]);
。
使用 transform
元素有利于应用缩放动画。
看看这个Jsfiddle。我调整了你的一些代码(没那么多,因为我只编辑了你的 css 动画)。
将背景分离到主横幅后面的独立元素中,这样您就可以应用关键帧动画来单独变换背景的比例。
body, html {
margin: 0;
padding: 0;
}
.site-space-background {
position: fixed; height: 100%; width: 100%;
background: #1A1939 no-repeat center center;
background-image: url(https://preview.ibb.co/m8kxST/static_banner_new.jpg);
background-attachment: fixed;
background-size: cover;
animation: shrink 5s infinite alternate steps(60);
}
@keyframes shrink {
0% {
transform: scale(1.2)
}
100% {
transform: scale(1.0)
}
}
<div class="site-space-background"></div>
<div class="site-main-banner">
<!-- The rest -->
</div>
我想随时间缩放我的 div 的背景图片,这样它看起来就像在放大和缩小(无需将鼠标悬停在上面 - 我只是在寻找动画自动启动)。但是,无论我尝试什么,背景动画看起来都是断断续续的。
如果我将 div 设置为缩放,那么过渡是平滑的,但它会缩放整个 div,而不仅仅是背景图像。有关更多信息,我是 运行 最新版本的 Chrome,并且已经尝试过 Windows 和 OS。你可以在这里看到一个例子:https://jsfiddle.net/sdqv19a0/
<div class="site-main-banner">
<div class="caption">
<!-- Bootstrap -->
<div class="container">
<div class="row">
<div class="col-xs-12">
<!-- figure -->
<figure> <img src="images/signature.png" alt="signature"> </figure>
<!-- H1 Heading -->
<h1 class="typewrite" data-period="2000" data-type='[ "Purposeful design", "Data-driven marketing", "Dynamic branding", "I love it all." ]'> <span class="wrap"></span> </h1>
<!-- H2 Heading -->
<h2>graphic designer • web designer • analytical marketer</h2>
<div class="clearfix"> </div>
<!-- Button -->
<a href="#" class="theme-btn">view my portfolio</a>
</div>
</div>
</div>
</div>
CSS:
.site-main-banner {
float:left;
width:100%;
background:url(https://preview.ibb.co/m8kxST/static_banner_new.jpg) no-repeat center center;
background-attachment:fixed;
background-size:cover;
margin: 0;
padding: 0;
display: block;
clear: both;
position: relative;
text-align:center;
overflow: hidden;
animation: shrink 5s infinite alternate steps(60);
}
@keyframes shrink {
0% {
background-size: 110% 110%;
}
100% {
background-size: 100% 100%;
}
}
.site-main-banner a.theme-btn {
color: #FFFFFF;
border:#FFFFFF solid 1px;
background:none;
box-shadow:none;
}
.site-main-banner a.theme-btn:hover {
color: #bc6ca7;
border:#FFFFFF solid 1px;
background:#FFFFFF;
box-shadow:none;
}
.site-main-banner .caption {
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%);
padding:300px 0;
}
.site-main-banner figure {
float:left;
width:100%;
text-align:center;
padding:0 0 15px 0;
}
建议?我也对 js 解决方案持开放态度,只是对 javascript 不太满意。谢谢!
我会将 css transform
属性 与伪元素结合使用。
.site-main-banner {
position: relative;
overflow: hidden;
}
.site-main-banner:after {
content: “”;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
z-index: -1;
background-image: url(...);
// put your animation here
}
我无法真正向您发送一个完整的示例,因为我目前正在地铁 phone,但我希望您能理解。
尝试在动画中使用 transform: scale([values]);
。
使用 transform
元素有利于应用缩放动画。
看看这个Jsfiddle。我调整了你的一些代码(没那么多,因为我只编辑了你的 css 动画)。
将背景分离到主横幅后面的独立元素中,这样您就可以应用关键帧动画来单独变换背景的比例。
body, html {
margin: 0;
padding: 0;
}
.site-space-background {
position: fixed; height: 100%; width: 100%;
background: #1A1939 no-repeat center center;
background-image: url(https://preview.ibb.co/m8kxST/static_banner_new.jpg);
background-attachment: fixed;
background-size: cover;
animation: shrink 5s infinite alternate steps(60);
}
@keyframes shrink {
0% {
transform: scale(1.2)
}
100% {
transform: scale(1.0)
}
}
<div class="site-space-background"></div>
<div class="site-main-banner">
<!-- The rest -->
</div>