- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在动态制作图像 slider (不想使用任何 硬编码
值)。我能够制作 slider ,但我有两个问题。
为什么我需要 20px
的 ul
宽度。有没有办法不使用这个硬编码值。
$('#list').css('width',$('#list').find('.box').length*li_Width+20)
next
按钮时框的某些部分 left
。例如,当用户单击 next
按钮时它显示 next
框不显示任何部分 (绿色框) ..但是当用户单击下一步按钮时它显示 蓝色
框的某些部分为什么?为什么它没有完全滑动?如果用户再次点击下一步按钮,它会再次显示红色的更多部分,为什么?这是我的代码 https://plnkr.co/edit/t2yOFkO1QBWOVLFreiXm?p=preview
//代码在这里
$(function(){
var li_Width =$('#list').find('.box:first').outerWidth(true);
// why 20 pixel is hardcorded
$('#list').css('width',$('#list').find('.box').length*li_Width+20)
$('#next').click(function(){
$('#list').css('margin-left',addToMarginLeft($('#list'),-li_Width))
})
$('#pre').click(function(){
$('#list').css('margin-left',addToMarginLeft($('#list'),li_Width))
})
function addToMarginLeft(elem, pixels) {
var ml = parseFloat(elem.css('margin-left'));
elem.animate({
'margin-left': (ml + pixels) + 'px'
},1000)
}
})
最佳答案
我认为这是由于 css inline-block
element 之间的空格造成的。这会导致一些额外的空间,所以你必须添加 20px。
有几种方法可以解决最后一个问题:在 li 之间进行注释或将字体大小设置为 0,如下所示:
ul {
...
font-size: 0
}
在下面找到工作片段:
我刚刚添加了一些 jquery 代码来防止多次点击按钮在最后一个动画完成之前触发另一个动画:
// Code goes here
$(function() {
var li_Width = $('#list').find('.box:first').outerWidth(true);
// 20 px removed !!!
$('#list').css('width', $('#list').find('.box').length * li_Width)
$('#next').click(function() {
if( !$("#list").is(":animated") )
$('#list').css('margin-left', addToMarginLeft($('#list'), -li_Width))
})
$('#pre').click(function() {
if( !$("#list").is(":animated") )
$('#list').css('margin-left', addToMarginLeft($('#list'), li_Width))
})
function addToMarginLeft(elem, pixels) {
var ml = parseFloat(elem.css('margin-left'));
elem.animate({
'margin-left': (ml + pixels) + 'px'
}, 1000)
}
})
/* Styles go here */
.box {
width: 100px;
height: 100px;
}
body {
margin: 0px;
padding: 0px;
}
.green {
background-color: green;
}
.red {
background-color: red;
}
.pink {
background-color: pink;
}
.yellow {
background-color: yellow;
}
.blue {
background-color: blue;
}
.orange {
background-color: orange;
}
ul {
list-style: none;
padding: 0px;
margin: 0px;
width: 1300px;
/* here you set font to 0 for extra space */
font-size: 0
}
li {
display: inline-block;
margin-left: 15px;
padding: 0;
}
#container {
width: 300px;
margin: 0 auto 0 auto;
overflow: hidden;
}
#list {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<button id="next">next</button>
<button id="pre">pre</button>
<ul id="list">
<li class="box green">
</li>
<li class="box blue">
</li>
<li class="box red">
</li>
<li class="box yellow">
</li>
<li class="box pink">
</li>
<li class="box orange">
</li>
</ul>
</div>
关于javascript - 为什么 margin 在 jquery 中被硬编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746010/
当我设置 margin-right: 50px;我没有看到任何效果,但是当我替换 margin-right: 50px; 时左边距:50px;或 margin-top: 50px;我确实看到了效果。这
CSS .title{ margin-top: 200px; // does not work! margin-left: 20px; font-weight: bold;
我不知道,但是 li 元素的 margin-top 只有在它大于 h2 元素的 margin-bottom 时才会起作用,我想知道为什么? Test1 Test2 谢谢。 最佳答案 您所描述的听起
我有 2 个 div 的问题 - 两个呈现为 block 的边距均为 15px(顶部 div 有底部边距,底部有顶部),因此我预计两者之间的差距是 30px 而不是 15px,这是正确的假设还是我要疯
我一直以为我了解利润率和负利润率,但显然我不了解!我刚刚开始一个新的设计并且已经遇到了问题。 我有一个 div (hill3Cont) 和另一个嵌套在里面的 div (hill3Hill),这是它们的
我有一系列这样的元素: ... ....... ... ....... h1 上边距为 5px,p 上边距为 10px。但是产生的边距只有 10px。如果我将底部边距增加到 50px,将顶部边距增加
由于 margin-right: auto 和 margin-left: auto 水平居中元素,我希望它们的垂直对应物以相同的方式运行。 但我知道这不会发生,根据 CSS 规范: 10.6.2 In
我在让我的 div 将我的页面向下移动 30 像素/在我的 div 顶部添加边距 30 像素时遇到问题。我的 div 使用 margin auto 在页面中心对齐。 然而,当我尝试添加这行代码时:边距
这个问题在这里已经有了答案: CSS margin terror; Margin adds space outside parent element [duplicate] (7 个答案) 关闭
我有一个 div 在另一个之上。顶部的 div 有 margin-bottom: 10px,底部的 div 有 margin-top: 10px,但两个 div 之间只有 10px 的空间。 实例:h
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 2 年前。 我的 CSS 边距没有按照我想要或期望的方
在此website我正在尝试减少横幅和文本之间ON MOBILE 底部和顶部的边距。 如果您在智能手机和平板电脑上查看,横幅的顶部和底部似乎有一些边距。 这是我的 CSS: .page-id-996
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
这个问题在这里已经有了答案: Why does this CSS margin-top style not work? (14 个答案) 关闭 3 年前。
我是一名优秀的程序员,十分优秀!