如何使用 JavaScript 从侧面滑入图像?

发布于 2024-11-04 06:12:22 字数 330 浏览 4 评论 0原文

我有一个显示图像的脚本,有点像轮播。图像放置在 LI 中,并且左侧位置根据每张幻灯片的宽度进行更改(全部相同)。目前,旧幻灯片消失,然后新幻灯片出现。

我想让它们从侧面滑入,并且想知道是否有人可以给我一个基本示例,说明如何使用纯 JavaScript(无 jQuery!) 来完成此操作。

例如,我使用以下代码来更新包含 UL 的左侧定位。我怎样才能做到这一点,以便它将所选图像向左或向右滑动(取决于是否单击下一个或上一个按钮)

containingUL.style.left = '-' + (slideNumber * slideWidth) + 'px';

I have a script that displays images sort of like a carousel. The images are placed in LIs and the left positioning is changed based on the width of each slide (all the same). Currently, the old slide just disappears then the new one appears.

I would like to make it so they slide in from the side and was wondering if someone could give me a basic example of how to do this using plain JavaScript (no jQuery!).

For example, I'm using the following code to update the left positioning of the containing UL. How can I make it so it will slide the selected image to the left or to the right (depending upon whether the next or previous button is clicked)

containingUL.style.left = '-' + (slideNumber * slideWidth) + 'px';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信愁 2024-11-11 06:12:22

这是一个基本的元素滑动功能。您可以使用 stepstimer 的值来获得恰到好处的动画速度和平滑度。

function slideTo(el, left) {
    var steps = 25;
    var timer = 25;
    var elLeft = parseInt(el.style.left) || 0;
    var diff = left - elLeft;
    var stepSize = diff / steps;
    console.log(stepSize, ", ", steps);

    function step() {
        elLeft += stepSize;
        el.style.left = elLeft + "px";
        if (--steps) {
            setTimeout(step, timer);
        }
    }
    step();
}

所以你可以去:

slideTo(containingUL, -slideNumber * slideWidth);

编辑:忘记链接到JSFiddle

编辑: 要向左滑动,请提供小于当前 style.leftleft 值。要向右滑动,请提供一个大于当前 style.left 的值。对你来说这应该不重要。您应该能够将其插入到您现有的代码中。我猜测您当前的代码会递增或递减 slideNumber ,然后根据 SlideNumber 设置 style.left 。像这样的东西应该可以工作:

if (nextButtonClicked) slideNumber++;
else slideNumber--;
slideTo(containingUL, -slideNumber * slideWidth);

我用滑动“画廊”的工作示例更新了 JSFiddle,包括上一个和下一个按钮。 http://jsfiddle.net/gilly3/EuzAK/2/

Here's a basic element slide function. You can play with the values of steps and timer to get the animation speed and smoothness just right.

function slideTo(el, left) {
    var steps = 25;
    var timer = 25;
    var elLeft = parseInt(el.style.left) || 0;
    var diff = left - elLeft;
    var stepSize = diff / steps;
    console.log(stepSize, ", ", steps);

    function step() {
        elLeft += stepSize;
        el.style.left = elLeft + "px";
        if (--steps) {
            setTimeout(step, timer);
        }
    }
    step();
}

So you could go:

slideTo(containingUL, -slideNumber * slideWidth);

Edit: Forgot to link to the JSFiddle

Edit: To slide to the left, provide a left value less than the current style.left. To slide to the right, provide a value greater than the current style.left. For you it shouldn't matter much. You should be able to plug it into your existing code. I'm guessing your current code either increments or decrements slideNumber and then sets style.left according to the slideNumber. Something like this should work:

if (nextButtonClicked) slideNumber++;
else slideNumber--;
slideTo(containingUL, -slideNumber * slideWidth);

I updated the JSFiddle with a working example of a sliding "gallery", including prev and next buttons. http://jsfiddle.net/gilly3/EuzAK/2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文