鼠标悬停时在轮播顶部设置绝对定位的 div 动画

发布于 2024-12-07 18:27:43 字数 384 浏览 0 评论 0原文

我有一个简单的轮播实现,我正在尝试对其进行一些调整以匹配一些非常具体的要求。

有一个绝对定位的覆盖层,当页面首次加载时突出显示轮播的第一个缩略图。

当鼠标移至轮播中的另一个可见缩略图时,我需要它滑动到该缩略图,并将叠加层中的文本更新为新图像编号(在 x 幅图像中) )。

现在,我花了将近一整天的时间试图让动画顺利进行。我尝试了多种方法,但我感到非常沮丧。这可能是因为我缺乏 CSS3 忍者能力。

这里是相关 HTML、CSS 和 jQuery 轮播实现的小提琴,需要 slidy- 的工作版本上面描述的四四方方的东西。

I've got a simple carousel implementation which I'm trying to tweak a bit to match some very specific requirements.

There is an absolutely positioned overlay which highlights the first thumbnail of the carousel when the page first loads up.

When the mouse is moved over to another visible thumbnail in the carousel, I need it to slide across to that thumbnail, and update the text within the overlay to the new image number (out of x number of images).

Now, I have spent nearly a full day trying to get the animation to happen nicely. I've tried with multiple approaches, and I'm getting quite frustrated. It is probably due to my lack of CSS3 ninja-ness.

Here is a fiddle for the relavant HTML, CSS and jQuery carousel implementation which needs a working version of the slidy-boxy thing described above.

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

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

发布评论

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

评论(1

安静被遗忘 2024-12-14 18:27:43

这是小提琴的更新版本 mouseover 事件的代码如下:

// References to commonly used elements
var $ul = $(".carousel ul");
var $overlayImageCounter = $('.overlayImageCounter');

// Original offset of the overlay.  Use it to correct the position we'll be
// moving it to over each image.
var offset = $overlayImageCounter.position().left;

// Mouseover binding go now!
$(".carousel img.galleryThumb").mouseover(function() {

    // The image that triggered our mouseover
    $img = $(this);

    // Animate the overlay to the image's left position.
    // Correct for carousel's current left margin and overlay's original offset
    $overlayImageCounter.stop().animate({
        left: $img.position().left + parseInt($ul.css('marginLeft'), 10) + offset
    });

    // Update the text of the overlay
    $imageCounter.text($ul.find('img').index($img) + 1 + "/" + nThumbs);

});

需要调整的代码的最后一点是重置轮播的上一个/下一个操作上的叠加层的左侧位置。我没有添加这一点,但这只是将 left 属性动画到其原始偏移量的问题。

Here's an updated version of the fiddle. Code is as follows for the <img> mouseover event:

// References to commonly used elements
var $ul = $(".carousel ul");
var $overlayImageCounter = $('.overlayImageCounter');

// Original offset of the overlay.  Use it to correct the position we'll be
// moving it to over each image.
var offset = $overlayImageCounter.position().left;

// Mouseover binding go now!
$(".carousel img.galleryThumb").mouseover(function() {

    // The image that triggered our mouseover
    $img = $(this);

    // Animate the overlay to the image's left position.
    // Correct for carousel's current left margin and overlay's original offset
    $overlayImageCounter.stop().animate({
        left: $img.position().left + parseInt($ul.css('marginLeft'), 10) + offset
    });

    // Update the text of the overlay
    $imageCounter.text($ul.find('img').index($img) + 1 + "/" + nThumbs);

});

The last bit of the code that would need to be tweaked is resetting the overlay's left position on the carousel's prev/next actions. I didn't add this bit but it's just a matter of animating the left property to its original offset.

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