我正在尝试用纯CSS移动div元素的背景图像。在这里我从用户输入背景图像网址,这就是为什么我不能在CSS中声明它。我也不能将它声明为具有固定位置或绝对位置的图像,因为它会影响我的其余CSS。有没有办法只为div元素的背景编写CSS?
小提琴 - jsfiddle.net/ZTsG9
HTML:
<div class="view" style="background-image: url('http://css3slideshow.remabledesigns.com/1.jpg')">According to a new report from AnandTech, Samsung might be fibbing its way to more favorable Galaxy S4 benchmarks. Has your device suddenly come to a crawl? Of course it hasn’t; benchmarks shouldn’t change your perception of a flagship as powerful as the S4. Still, it’s embarrassing that Samsung would resort to such technical tactics, like allegedly using code dubbed “BenchmarkBooster.” Yes, your device takes steroids.</div>
CSS:
.view {
position:relative;
height:100%;
width:100%;
top:0;
left:0;
animation:mymove 5s infinite;
-webkit-animation:mymove 5s infinite;
/* Safari and Chrome */
}
@keyframes mymove {
from {
left:0px;
}
to {
left:200px;
}
}
@-webkit-keyframes mymove
/* Safari and Chrome */
{
from {
leftp:0px;
}
to {
left:200px;
}
}
答案 0 :(得分:1)
你需要做的是添加background-position:x,y,其中x和y等于你想要定位元素的位置的长度和高度。 http://www.w3schools.com/cssref/pr_background-position.asp
<div class="view" style="background-image: url('http://css3slideshow.remabledesigns.com/1.jpg') background-position:50px 20px;">
According to a new report from AnandTech, Samsung might be fibbing its way to more favorable Galaxy S4 benchmarks. Has your device suddenly come to a crawl? Of course it hasn’t; benchmarks shouldn’t change your perception of a flagship as powerful as the S4. Still, it’s embarrassing that Samsung would resort to such technical tactics, like allegedly using code dubbed “BenchmarkBooster.” Yes, your device takes steroids.</div>
答案 1 :(得分:0)
以下技巧似乎有效:将背景图像的宽度设置为元素的两倍,并将背景位置的水平部分从0更改为-200%
(edited FIDDLE):
.view {
height:100%;
width:100%;
animation:mymove 5s linear infinite;
background-size: 200% 100%; /* horizontal scale is 200% */
}
@keyframes mymove {
from {
background-position: 0% 0px;
}
to {
background-position: -200% 0px; /* shifting the image to right by its full width */
}
}
background-position
为负百分比时移动图像的算法很好地解释了in this answer。