我想使用TwentyTwenty.js代码来比较图像。这一切都是伟大的工作,除了图像是对齐左侧。如何在不显式设置容器宽度的情况下将它们保持在中心位置?
<div id="beforeafter" class="twentytwenty-container">
<img src="https://lorempixel.com/800/300/sports/2/">
<img src="https://lorempixel.com/800/300/sports/3"/>
</div>
$(window).load(function() {
$("#beforeafter").twentytwenty();
});http://codepen.io/anon/pen/EmEYjQ
如果我在容器上设置固定宽度,则图像位于中心,但我希望图像不限于宽度,但它们不比屏幕宽的情况除外。
如果我用的是挠性对中,手柄不在中间
display: flex;
justify-content: center;发布于 2017-05-11 01:14:17
我也经历过一段艰难的日子!
我花了很多时间在所有我能找到的与之相关的CSS上。
终于..。以下是一个简单的解决方案:
$(window).load(function() {
// Was there.
$("#beforeafter").twentytwenty();
// Mesure your images and divide by 2.
var imgWidth = $("#beforeafter img").width()/2;
// On the container, apply a left offset of 50% (screen center) - minus half image width.
$("#beforeafter").css({"position":"relative","left":"calc(50% - "+ imgWidth+ "px)"});
});CodePen
发布于 2019-04-08 11:54:56
基本上在jquery.twentytwenty.js的第48行中添加:
container.css("height", offset.h);
container.css("max-width", offset.w); // See https://github.com/zurb/twentytwenty/issues/50https://stackoverflow.com/questions/43903986
复制相似问题