旋转图像和文本

发布于 2024-11-16 16:26:00 字数 180 浏览 0 评论 0原文

我希望在我的网站上每隔几秒钟旋转一张图像及其相应的文本块。就像这些人 http://hellofisher.com/

我知道我可以使用 javascript 来旋转图像,但我还没有找不到可以让文本块同时旋转以适应图像的地方。

I want to have an image with it's corresponding block of text rotating every few seconds on my website. Just like these guys http://hellofisher.com/

I know I can get a javascript to rotate the images but I haven't found where I can have the block of text alongside rotating to suit the images.

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

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

发布评论

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

评论(4

瑕疵 2024-11-23 16:26:00

只需谷歌搜索 jquery 内容旋转器或内容滑块即可。有一百万个现成的插件已经这样做了:

例如

Just google for jquery content rotators or content sliders. There's a million ready-made plugins already done like this:

For example

童话里做英雄 2024-11-23 16:26:00

我将向您展示一个简单的示例,您可以轻松地在您的网站上使用它。此示例使用 3 张幻灯片,并不是动态滑块,您可以拥有任意数量的幻灯片,但如果幻灯片数量可以变化,则可以轻松将其编辑为一张幻灯片。

HTML:

<div id="slider">
    <div id="slider-area">
        <div class="slide">content</div>
        <div class="slide">content</div>
        <div class="slide">content</div>
    </div>
</div>

滑块是用户可以看到的可见区域,滑块区域是放置幻灯片的区域,每张幻灯片包含不同的图像和文本。

CSS:

#slider {
    width: 400px;
    height: 400px;
    overflow: hidden;
    position: relative;
}

#slider-area {
    width: 1200px;
    height: 400px;
    position: absolute;
    top: 0;
    left: 0;
}

.slide {
    width: 400px;
    float: left;
}

如您所见,滑块区域的宽度是滑块大小的 3 倍,因为有 3 张幻灯片。滑块有overflow:hidden,这意味着滚动条不可见,还有position:relative,这样我们就可以绝对地将滑块区域定位在滑块内。然后我们只需将幻灯片插入滑块区域即可。

JS(使用 JQuery):

$(document).ready(function() {
    var timer = setInterval(changeSlide, 5000);
});

function changeSlide() {
    var pos = $('#slider').attr('scrollLeft');
    if (pos==800)
        pos=0
    else
        pos=pos+400;
    $('#slider').animate({scrollLeft: pos}, 600, 'swing');
}

在这里您设置一个计时器,它将每 5 秒调用一次changeSlide 函数。 ChangeSlide 函数将获取滑块滚动条的当前位置并相应地设置动画。如果显示最后一张幻灯片,下一张将是第一张幻灯片 (pos=0),如果不显示最后一张幻灯片,则会在位置上添加 400,以便滚动到下一张幻灯片。基本上,滑动动画只是用计时器移动隐藏的滚动条。

希望这有帮助!

I'll show you a simple example which you can easily use on your site. This example uses 3 slides and isn't a dynamic slider where you can have as many slides as you want to but it's easy to edit it into one if the amount of slides can vary.

HTML:

<div id="slider">
    <div id="slider-area">
        <div class="slide">content</div>
        <div class="slide">content</div>
        <div class="slide">content</div>
    </div>
</div>

Slider is the visible area which the user can see, slider-area is the area where you put the slides and each slide contains a different image and text.

CSS:

#slider {
    width: 400px;
    height: 400px;
    overflow: hidden;
    position: relative;
}

#slider-area {
    width: 1200px;
    height: 400px;
    position: absolute;
    top: 0;
    left: 0;
}

.slide {
    width: 400px;
    float: left;
}

As you can see, slider-area's width is 3 times the size of slider because there are 3 slides. Slider has overflow: hidden which means scrollbar won't be visible and position: relative so that we can absolutely position the slider-area inside the slider. Then we simply insert slides inside the slider-area.

JS (using JQuery):

$(document).ready(function() {
    var timer = setInterval(changeSlide, 5000);
});

function changeSlide() {
    var pos = $('#slider').attr('scrollLeft');
    if (pos==800)
        pos=0
    else
        pos=pos+400;
    $('#slider').animate({scrollLeft: pos}, 600, 'swing');
}

Here you set a timer which will call the changeSlide function every 5 seconds. The changeSlide function will take the current position of the slider's scrollbar and animate it accordingly. If it's displaying the last slide, next one will be the first slide (pos=0) and if it's not displaying the last slide, it adds 400 to the position so that it will scroll to the next slide. Basically the sliding animation is simply moving a hidden scrollbar with a timer.

Hopefully that helps!

隔纱相望 2024-11-23 16:26:00

只需使用 jQuery 旋转出包含图像和关联文本的

即可。使用 $.hide()$.show() 以及 Javascript 的时间函数。

Just use jQuery to rotate out a <div> containing an image and associated text. Use $.hide() and $.show() and Javascript's time functions.

少钕鈤記 2024-11-23 16:26:00

您可以尝试:

JAVASCRIPT

var imagelist = ["IMAGE1","IMAGE2","IMAGE3"]
var textlist = ["TEXT1","TEXT2","TEXT3"];
counter=0;
countmax=3;
var next = function(){
    $('rotating_image').innerHTML="<img src=\"" imagelist[counter%countmax] + "\">";
    $('rotating_text').innerHTML=textlist[counter%countmax];
    countmax++;
}
var rotate = window.setInterval(next, SECONDS * 1000);

HTML

<span id="rotating_image"></span>
<span id="rotating_text"></span>

You could try:

JAVASCRIPT

var imagelist = ["IMAGE1","IMAGE2","IMAGE3"]
var textlist = ["TEXT1","TEXT2","TEXT3"];
counter=0;
countmax=3;
var next = function(){
    $('rotating_image').innerHTML="<img src=\"" imagelist[counter%countmax] + "\">";
    $('rotating_text').innerHTML=textlist[counter%countmax];
    countmax++;
}
var rotate = window.setInterval(next, SECONDS * 1000);

HTML

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