同时使用background-attachment:fixed; 和 background-size:cover;时背景图大小被切割的解决方法

想要的效果:网站banner图片,在往下拉滚动条的时候,希望保持不动,下面的内容从它上面盖过去。


<div id="front-index-banner">
	<div class="bd">
		<ul>
			<li style="background-image:url('/lib/dist/images/front/banner.jpg')">
			<div class="bannercon">
				<div class="bannertip">
					<div class="wow">
						<img src="/lib/dist/images/front/banner_tip.png" />
					</div>
				</div>
			</div>
			</li>
			<li style="background-image:url('/lib/dist/images/front/banner.jpg');position:relative;">
				<a href="<{link app=site ctl=single act=article args=array(8)}>" style="display:block;position:absolute;left:50%;top:0;margin-left:-610px;width:1220px;height:677px;"></a>
			</li>
		</ul>
	</div>
	<div class="hd">
		<ul></ul>
	</div>
</div>

我使用的是SuperSlide切换效果。css和js文件如下:


$("#front-index-banner").slide({titCell:".hd ul",mainCell:".bd ul",effect:"fold",delayTime:1000,interTime:5000,trigger:"mouseover",autoPage:true,autoPlay:true});

#front-index-banner { width: 100%; height:677px; overflow: hidden;  position:relative;}
#front-index-banner .bd { width: 100%; overflow: hidden;}
#front-index-banner .bd li { float: left; width: 100%; height: 677px; background-position: center 0; background-repeat: no-repeat;background-attachment: scroll;background-size:cover;}
#front-index-banner .bd li .bannercon { text-align: center; padding-top: 240px;  position:relative;}
#front-index-banner .bd li .bannertip{ text-align:center; position:absolute;left:50%;margin-left:-420px;width:840px;top:261px;}
#front-index-banner .hd ul { position: absolute; width: 100px; left: 50%; bottom: 24px; margin-left: -50px;}
#front-index-banner .hd ul li { float: left; margin-right: 8px; width: 12px; height:12px; line-height: 12px; background-color: #fff; text-indent: -9999px; overflow: hidden; border-radius: 50%; cursor: pointer;}
#front-index-banner .hd ul li.on { background-color: #a02200;}


注意加粗部分,如果这里使用 background-attachment: fixed;background-size:cover; 是可以达到效果的,但是图片会被裁减。

所以修改为

background-attachment: scroll;background-size:cover;

然后加上下面的JS代码即可实现同样的效果,而且图片显示正常。

$(window).scroll(function() {
  var scrolledY = $(window).scrollTop();
  $('#front-index-banner .bd li').css('background-position', 'left ' + ((scrolledY)) + 'px');
});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值