Javascript 如何建立滑动水平菜单。CSS
我试图构建一个简单的滑块,它由一个静态的“窗口”和一个可移动的项目列表组成 其中父容器仅显示一个项,而隐藏所有其余项。 我尝试过这样做,但似乎这是错误的:Javascript 如何建立滑动水平菜单。CSS,javascript,html,css,Javascript,Html,Css,我试图构建一个简单的滑块,它由一个静态的“窗口”和一个可移动的项目列表组成 其中父容器仅显示一个项,而隐藏所有其余项。 我尝试过这样做,但似乎这是错误的: <div id="category-selector"> <div class="categories-list clearfix"> <a class="category">sports</a> <a class="category">fa
<div id="category-selector">
<div class="categories-list clearfix">
<a class="category">sports</a>
<a class="category">fashion</a>
<a class="category">health</a>
</div>
</div>
#category-selector {
width: 300px; margin: 0 auto; position: relative; z-index: 1;
border: 2px solid #ccc;
-moz-box-sizing: content-box; -webkit-box-sizing: content-box; box-sizing: content-box; height: 55px;
overflow: hidden;
}
.categories-list {
position: absolute; top: 0; left: 0; display: block;
}
a.category {
display: block; float: left; width: 100%; padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
体育
时尚
健康
#类别选择器{
宽度:300px;边距:0自动;位置:相对;z索引:1;
边框:2个实心#ccc;
-moz框大小:内容框;-webkit框大小:内容框;框大小:内容框;高度:55px;
溢出:隐藏;
}
.类别清单{
位置:绝对;顶部:0;左侧:0;显示:块;
}
a、 类别{
显示:块;浮动:左;宽度:100%;填充:10px;
字体大小:30px;字体系列:Cambria,'Segoe UI',无衬线;线条高度:35px;
文字装饰:无;文字对齐:居中;颜色:#42a6ce;
}
如何实现此功能?尝试以下方法:
.categories-list {
display: block;
white-space: nowrap;
/*margin-left: -300px;*/
}
a.category {
display: inline-block;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
如果希望链接从左到右排列,则应将其设置为固定宽度。如果您设置为100%,则他们将始终尝试填充容器。将display
设置为inline block
允许我们通过设置空白:nowrap来避免换行代码>在容器上。
要滚动它,只需在容器上设置边距,例如marginleft:-300px代码>
工作样本:
或者,您可以尝试以下方法:
.categories-list {
display: block;
white-space: nowrap;
margin-left: -300px;
width: 10000px; /* long enough to fit all links */
}
a.category {
display: block;
float:left;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
这在您的尝试中使用了类似的display:block
和float:left
,但宽度是固定的。若要将所有链接放在一行中,类别列表必须比所有链接一起更宽
工作示例:尝试以下操作:
.categories-list {
display: block;
white-space: nowrap;
/*margin-left: -300px;*/
}
a.category {
display: inline-block;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
如果希望链接从左到右排列,则应将其设置为固定宽度。如果您设置为100%,则他们将始终尝试填充容器。将display
设置为inline block
允许我们通过设置空白:nowrap来避免换行代码>在容器上。
要滚动它,只需在容器上设置边距,例如marginleft:-300px代码>
工作样本:
或者,您可以尝试以下方法:
.categories-list {
display: block;
white-space: nowrap;
margin-left: -300px;
width: 10000px; /* long enough to fit all links */
}
a.category {
display: block;
float:left;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
这在您的尝试中使用了类似的display:block
和float:left
,但宽度是固定的。若要将所有链接放在一行中,类别列表必须比所有链接一起更宽
工作示例:尝试以下操作:
.categories-list {
display: block;
white-space: nowrap;
/*margin-left: -300px;*/
}
a.category {
display: inline-block;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
如果希望链接从左到右排列,则应将其设置为固定宽度。如果您设置为100%,则他们将始终尝试填充容器。将display
设置为inline block
允许我们通过设置空白:nowrap来避免换行代码>在容器上。
要滚动它,只需在容器上设置边距,例如marginleft:-300px代码>
工作样本:
或者,您可以尝试以下方法:
.categories-list {
display: block;
white-space: nowrap;
margin-left: -300px;
width: 10000px; /* long enough to fit all links */
}
a.category {
display: block;
float:left;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
这在您的尝试中使用了类似的display:block
和float:left
,但宽度是固定的。若要将所有链接放在一行中,类别列表必须比所有链接一起更宽
工作示例:尝试以下操作:
.categories-list {
display: block;
white-space: nowrap;
/*margin-left: -300px;*/
}
a.category {
display: inline-block;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
如果希望链接从左到右排列,则应将其设置为固定宽度。如果您设置为100%,则他们将始终尝试填充容器。将display
设置为inline block
允许我们通过设置空白:nowrap来避免换行代码>在容器上。
要滚动它,只需在容器上设置边距,例如marginleft:-300px代码>
工作样本:
或者,您可以尝试以下方法:
.categories-list {
display: block;
white-space: nowrap;
margin-left: -300px;
width: 10000px; /* long enough to fit all links */
}
a.category {
display: block;
float:left;
width: 280px;
padding: 10px;
font-size: 30px; font-family: Cambria, 'Segoe UI', sans-serif; line-height: 35px;
text-decoration: none; text-align: center; color: #42a6ce;
}
这在您的尝试中使用了类似的display:block
和float:left
,但宽度是固定的。若要将所有链接放在一行中,类别列表必须比所有链接一起更宽
工作示例:如果您不介意使用JS或按钮,这是一种方法
$(document).ready(function() {
var slider = $("#categoriese_list");
var leftProperty, newleftProperty;
// the click event handler for the right button
$("#right_button").click(function() {
// get value of current left property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty - 100 <= -900) {
newLeftProperty = 0; }
else {
newLeftProperty = leftProperty - 100; }
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
// the click event handler for the left button
$("#left_button").click(function() {
// get value of current right property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty < 0) {
newLeftProperty = leftProperty + 100;
}
else {
newLeftProperty = -800;
}
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
}); // end ready
$(文档).ready(函数(){
变量滑块=$(“#分类列表”);
var leftProperty、newleftProperty;
//单击右按钮的事件处理程序
$(“#右键”)。单击(函数(){
//获取当前左属性的值
leftProperty=parseInt(slider.css(“left”);
//确定left属性的新值
如果(leftProperty-100如果您不介意使用JS或按钮,这是一种方法
$(document).ready(function() {
var slider = $("#categoriese_list");
var leftProperty, newleftProperty;
// the click event handler for the right button
$("#right_button").click(function() {
// get value of current left property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty - 100 <= -900) {
newLeftProperty = 0; }
else {
newLeftProperty = leftProperty - 100; }
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
// the click event handler for the left button
$("#left_button").click(function() {
// get value of current right property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty < 0) {
newLeftProperty = leftProperty + 100;
}
else {
newLeftProperty = -800;
}
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
}); // end ready
$(文档).ready(函数(){
变量滑块=$(“#分类列表”);
var leftProperty、newleftProperty;
//单击右按钮的事件处理程序
$(“#右键”)。单击(函数(){
//获取当前左属性的值
leftProperty=parseInt(slider.css(“left”);
//确定left属性的新值
如果(leftProperty-100如果您不介意使用JS或按钮,这是一种方法
$(document).ready(function() {
var slider = $("#categoriese_list");
var leftProperty, newleftProperty;
// the click event handler for the right button
$("#right_button").click(function() {
// get value of current left property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty - 100 <= -900) {
newLeftProperty = 0; }
else {
newLeftProperty = leftProperty - 100; }
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
// the click event handler for the left button
$("#left_button").click(function() {
// get value of current right property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty < 0) {
newLeftProperty = leftProperty + 100;
}
else {
newLeftProperty = -800;
}
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
}); // end ready
$(文档).ready(函数(){
变量滑块=$(“#分类列表”);
var leftProperty、newleftProperty;
//单击右按钮的事件处理程序
$(“#右键”)。单击(函数(){
//获取当前左属性的值
leftProperty=parseInt(slider.css(“left”);
//确定left属性的新值
如果(leftProperty-100如果您不介意使用JS或按钮,这是一种方法
$(document).ready(function() {
var slider = $("#categoriese_list");
var leftProperty, newleftProperty;
// the click event handler for the right button
$("#right_button").click(function() {
// get value of current left property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty - 100 <= -900) {
newLeftProperty = 0; }
else {
newLeftProperty = leftProperty - 100; }
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
// the click event handler for the left button
$("#left_button").click(function() {
// get value of current right property
leftProperty = parseInt(slider.css("left"));
// determine new value of left property
if (leftProperty < 0) {
newLeftProperty = leftProperty + 100;
}
else {
newLeftProperty = -800;
}
// use the animate function to change the left property
slider.animate( {left: newLeftProperty}, 1000);
}); // end click
}); // end ready
$(文档).ready(函数(){
变量滑块=$(“#分类列表”);
var leftProperty、newleftProperty;
//单击右按钮的事件处理程序
$(“#右键”)。单击(函数(){
//获取当前左属性的值
leftProperty=parseInt(slider.css(“left”);
//确定left属性的新值
if(leftProperty-100你所说的本质上是一个旋转木马或滑块。与其尝试从头开始编写代码,我只想使用一百万个jQuery插件中的一个来构建它。我个人非常喜欢这样的东西,因为它响应迅速,实现起来非常简单。你所说的是