老师,我这样写是不是也对,也可以吧
来源:2-3 代码编写(1)
张小阳_
2022-09-30 00:33:49
(function () {
var carousel_list = document.getElementById('carousel_list');
var left_arrow = document.getElementById('left_arrow');
var right_arrow = document.getElementById('right_arrow');
// 先复制假图
var clone_img = carousel_list.firstElementChild.cloneNode(true);
carousel_list.appendChild(clone_img);
var idx = 0;
right_arrow.onclick = function () {
idx++;
carousel_list.style.transition = 'transform 1s 0s ease';
if (idx > 4) {
setTimeout(function () {
idx = 0;
carousel_list.style.transform = 'translateX(' + 0 + '%)';
carousel_list.style.transition = 'none';
}, 1000);
}
carousel_list.style.transform = 'translateX(' + idx * -16.66 + '%)';
};
// 左按钮
left_arrow.onclick = function () {
idx--;
if (idx == -1) {
idx = 5
carousel_list.style.transform = 'translateX(' + idx * -16.66 + '%)';
carousel_list.style.transition = 'none';
setTimeout(function () {
idx = 4;
carousel_list.style.transform = 'translateX(' + idx * -16.66 + '%)';
carousel_list.style.transition = 'transform 1s 0s ease';
}, 0);
} else {
carousel_list.style.transform = 'translateX(' + idx * -16.66 + '%)';
}
};
})(); //为了不产生额外的全局变量,而污染全局变量所以,这里使用IIFE1回答
同学你好,如果同学的代码可以实现效果就可以了。建议以后将涉及到的代码一并贴过来,这样老师可以在原有的基础上帮忙测试一下效果。
祝学习愉快!
相似问题