如何将img维度从html移动到css并保持此滑块工作
how can i move the img dimensions from html to css and keep this slider working?
我有这个前后滑块(第一个内联图像)http://whitewashed.richiesiegel.com/我想将图像维度属性移到css中,但这样做会中断。知道怎么解决吗?
js小提琴在这里http://jsfiddle.net/85jtY/
HTML
<div class="before_after_slider">
<div class="photo">
<div class="after">
<img src="http://whitewashed.richiesiegel.com/images/cans.jpg" width="1000px" height="600px" alt="after" />
</div>
<div class="before">
<img src="http://whitewashed.richiesiegel.com/center_after.jpg" width="1000px" height="600px" alt="before" />
</div>
</div>
</div>
<div class="caption">
<p>Roll over the photo with your cursor to see the before/after images. </p>
</div>
CSS
.before_after_slider {
position: relative;
padding-bottom: 10px;
width:1000px;
margin: 0 auto;
& > * {
position: relative;
}
}
.after {
overflow: hidden;
position: absolute;
top: 0px;
width:1000px;
height:600px;
}
.caption {
font-family: helvetica;
font-weight: 100;
line-height: 140%;
letter-spacing: 0px;
font-size: 13px;
width:1000px;
margin: 0 auto;
padding-top: 0px;
padding-bottom: 30px;
}
JAVASCRIPT:
$(document).ready(function(){
var $after = $('.after'),
img_width = $('.after img').width(),
init_split = Math.round(img_width/2);
$after.width(init_split);
$('.before_after_slider').mousemove(function(e){
var offX = (e.offsetX || e.clientX - $after.offset().left);
$after.width(offX);
});
$('.before_after_slider').mouseleave(function(e){
$after.stop().animate({
width: init_split
},200)
});
});
fiddle
.pic{
width:100%;
height:100%;
}
将类添加到img属性
您的javascript的这一行。。。
img_width = $('.after img').width()
正在获取img
元素的宽度,但css中没有设置img
元素的宽度而是仅设置包装div
元素的img
元素。所以你需要显式地设置宽度,比如…
.before img,
.after img {
width: 1000px;
height: 600px;
}
或者(类似于Sai Ram Sudheer的答案,但没有添加额外的类别),就像这样…
.before img,
.after img {
width: 100%;
height: 100%;
}
其利用包装器div维度来确定图像的大小。关键是,您必须实际给img
本身一个大小,而不仅仅是img
的包装器,$('.after img').width()
函数调用才能工作。然后,您应该能够消除html中的设置。
相关文章:
- 为什么不'在JQuery中找到第二个css选择器的工作
- jquery滑块在css弹出菜单中工作
- JavaScript在更改css时停止工作
- 链接悬停css在不浮动时不在表浮动标题中工作
- 如何使Javascript中的CSS正常工作
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- 为什么不是't my css `content:`在多个浏览器中工作,并总体上提高跨浏览器兼容性
- 如何使手风琴(CSS+javascript)工作
- CSS + jQ插件阻止AJAX调用/事件侦听器工作
- CSS 类在验证时只工作一次
- 如何在HTML / CSS中填充垂直和滚动中间窗格(Chrome可以工作,但不能Firefox)
- jQuery css"点击“;不会'更改日历中选定的月份后无法工作
- CSS没有'使用javascript向页面动态添加元素时无法工作
- JQuery图像滑块和CSS转换只能工作一次
- CSS和Javascript可以在内部服务器上工作,但不能在外部工作
- CSS,Java脚本在php中调用ajax后无法正常工作
- 如何将img维度从html移动到css并保持此滑块工作
- 如何在网页上以交互方式切换 CSS 工作表以进行测试/学习
- 悬停淡出不与砌体CSS工作
- 自动cookie条纯CSS工作良好,但需要延迟