【发布时间】:2016-09-21 20:07:31
【问题描述】:
我想知道如何根据星期几设置.scrollLeft()。
我发现我可以使用Date().getdate() 获取工作日号码。星期天返回 0,星期六返回 6。
但是,我正在制定一个水平滚动的时间表。时间表位于 700% 宽度 div 中。星期一从left: 0 开始。如果用户在星期二单击,则 div 向右滚动 100% 以显示星期二。到目前为止一切正常,尽管我的 jQuery 可能更直接一些(目前我使用eq(0)、eq(1)、eq(2) 等循环遍历按钮)。
$(document).ready(function () {
$(".daypick li a").eq(0).on("click", function (event) {
event.preventDefault();
$(".calendar").css({left: 0});
});
$(".daypick li a").eq(1).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-100%"});
});
$(".daypick li a").eq(2).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-200%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
$(".daypick li a").eq(3).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-300%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
$(".daypick li a").eq(4).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-400%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
$(".daypick li a").eq(5).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-500%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
$(".daypick li a").eq(6).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-600%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
$(".daypick li a").eq(7).on("click", function (event) {
event.preventDefault(); //Prevent default action of anchor
$(".calendar").css({left: "-700%"}); //Animates the scroll
/* Can also use 3000, 4000 i.e 3 seconds, 4 seconds to animate the scroll */
});
});
我在这里想要实现的是脚本检查它是哪一天并显示那一天的时间表。所以在星期三父 div 应该得到left: -300%,第三个按钮应该得到类active。
但是,我对日期有点犹豫。我的第一个想法是取工作日数字并乘以 100 以获得 left 值。问题是星期天返回 0,在我的情况下,我需要星期一返回 0 才能正常工作。
希望有人可以提供一些帮助。
我创建了一个jsfiddle here,
【问题讨论】:
-
只需创建一个函数将您得到的“天数”“翻译”成您期望的“天数”。