MooTools 图像列表

发布于 2024-11-03 05:07:13 字数 538 浏览 2 评论 0原文

我有这个 HTML:

<div id='cont'>
    <img src='1.jpg' />
    <img src='2.jpg' />
    <img src='2.jpg' />
</div>

<a href='#'>Prev</a>
<a href='#'>Next</a>

使用这个 CSS:

#cont{
    position: relative;
    float: left;
    height: 90px;
    width: 120px;
}

#cont img {
    display:block;
    position:absolute;
    top:0;
    left:0;
    z-index: 1;
}

单击 NextPrev 我需要显示图像。使用 MooTools 执行此操作的最简单方法是什么?

I have this HTML:

<div id='cont'>
    <img src='1.jpg' />
    <img src='2.jpg' />
    <img src='2.jpg' />
</div>

<a href='#'>Prev</a>
<a href='#'>Next</a>

with this CSS:

#cont{
    position: relative;
    float: left;
    height: 90px;
    width: 120px;
}

#cont img {
    display:block;
    position:absolute;
    top:0;
    left:0;
    z-index: 1;
}

clicking Next and Prev I need to show images. What is the easiest way to do this with MooTools?

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

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

发布评论

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

评论(1

深海里的那抹蓝 2024-11-10 05:07:13

奇怪的是,昨天刚刚在我的博客上写了一篇关于如何在 mootools 中执行此操作的教程。

http://fragged .org/tutorial-write-a-small-content-slider-class-in-mootools-and-extend-it_1321.html

实际代码适合与任何类型的绝对元素,因此它可以轻松地为您工作。下一个和上一个是图像,但您可以用锚点替换它们。

这里有两个类(基本类和扩展类,用于不同的效果):

this.contentSwapper = new Class({

    Implements: [Options, Events],

    options: {
        delay: 3000,
        selector: "div",
        controlLeft: "http://fragged.org/img/home/moveLeft.png",
        controlRight: "http://fragged.org/img/home/moveRight.png"
    },

    initialize: function(element, options) {
        this.element = document.id(element);
        if (!this.element)
            return;

        this.setOptions(options);

        this.elements = this.element.getChildren(this.options.selector);
        this.index = 0;
        this.attachControls();
        this.startRotation();
        this.attachEvents();
        this.fireEvent("ready");
    },

    attachControls: function() {
        this.controls = $([
            new Element("img#moveLeft.contentControl[title=Previous][src={controlLeft}]".substitute(this.options)).inject(this.element, "top"),
            new Element("img#moveRight.contentControl[title=Next][src={controlRight}]".substitute(this.options)).inject(this.element, "top")
        ]);
    },


    attachEvents: function() {
        this.element.addEvents({
            mouseenter: this.stopRotation.bind(this),
            mouseleave: this.startRotation.bind(this),
            "click:relay(img.contentControl)": this.move.bind(this)
        });
    },

    move: function(e, el) {
        this[el.get("id")]();
    },

    moveLeft: function() {
        var next = (this.index == 0) ? this.elements.length-1 : this.index-1;
        this.swapFrames(next);
        this.fireEvent("left");
    },

    moveRight: function() {
        var next = (this.index < this.elements.length-1) ? this.index+1 : 0;
        this.swapFrames(next);
        this.fireEvent("right");
    },

    swapFrames: function(next) {
        // internal to do the swaps
        var curEl = this.elements[this.index];
        curEl.get("tween").removeEvents();
        curEl.set({
            "tween": {
                link: "cancel",
                onComplete: function() {
                    this.element.addClass("hide");
                }
            },
            styles: {
                opacity: 1
            }
        }).fade(0);

        this.index = next;

        var newEl = this.elements[this.index];
        newEl.get("tween").removeEvents();
        newEl.setStyle("opacity", 0).removeClass("hide").fade(1);
    }.protect(),

    stopRotation: function() {
        clearInterval(this.timer);
        this.controls.fade(1);
        this.fireEvent("stop");
    },

    startRotation: function() {
        clearInterval(this.timer);
        this.controls.fade(.5);
        this.timer = this.moveRight.periodical(this.options.delay, this);
        this.fireEvent("start");
    }
});


contentSwapper.Fancy = new Class({

    Extends: contentSwapper,

    initialize: function(element, options) {
        this.parent(element, options);
    },

    swapFrames: function(next) {
        var curEl = this.elements[this.index];
        curEl.get("morph").removeEvents();
        curEl.set({
            styles: {
                zIndex: 1000,
                opacity: 1
            },
            "morph": {
                link: "cancel",
                duration: 1000,
                onComplete: function() {
                    this.element.addClass("hide");
                }
            }
        }).morph({
            opacity: 0
        });

        this.index = next;

        var newEl = this.elements[this.index];
        newEl.get("morph").removeEvents();
        newEl.removeClass("hide").setStyles({
            zIndex: 1001,
            marginTop: -280,
            opacity: 0
        }).morph({
            marginTop: 0,
            opacity: 1
        });
    }

});

new contentSwapper.Fancy(document.id("rotator"));

html:

<section class="rotator" id="rotator">
    <div class="pane" style="background-image:url(http://fragged.org/img/home/hunting-home.jpg)">
        <div class="info">
            <h2>Hunting CS style</h2>
            Built for your `camping` pleasure. nOOb
        </div>
    </div>
    <div class="pane hide" style="background-image:url(http://fragged.org/img/home/fishing-home.jpg)">
        <div class="info">
            <h2>Fishing? Really? </h2>
            Fishing is for mongs. <a href="#">Clcik here</a>
        </div>
    </div>
    <div class="pane hide" style="background-image:url(http://fragged.org/img/home/tourism-home.jpg)">
       <div class="info rambling">
           <h2>Rambling and walking</h2>
           Wish you were here? Can't blame you, it's lame.
        </div>
    </div>
</section>

等等等,玩得开心

oddly enough, just wrote a tutorial on how to do this in mootools on my blog yesterday.

http://fragged.org/tutorial-write-a-small-content-slider-class-in-mootools-and-extend-it_1321.html

the actual code is geared up to work with any kind of absolute elements so it can easily work for you. next and previous are images but you can replace them with anchors.

here are the two classes (the base and the extended one for the different effect):

this.contentSwapper = new Class({

    Implements: [Options, Events],

    options: {
        delay: 3000,
        selector: "div",
        controlLeft: "http://fragged.org/img/home/moveLeft.png",
        controlRight: "http://fragged.org/img/home/moveRight.png"
    },

    initialize: function(element, options) {
        this.element = document.id(element);
        if (!this.element)
            return;

        this.setOptions(options);

        this.elements = this.element.getChildren(this.options.selector);
        this.index = 0;
        this.attachControls();
        this.startRotation();
        this.attachEvents();
        this.fireEvent("ready");
    },

    attachControls: function() {
        this.controls = $([
            new Element("img#moveLeft.contentControl[title=Previous][src={controlLeft}]".substitute(this.options)).inject(this.element, "top"),
            new Element("img#moveRight.contentControl[title=Next][src={controlRight}]".substitute(this.options)).inject(this.element, "top")
        ]);
    },


    attachEvents: function() {
        this.element.addEvents({
            mouseenter: this.stopRotation.bind(this),
            mouseleave: this.startRotation.bind(this),
            "click:relay(img.contentControl)": this.move.bind(this)
        });
    },

    move: function(e, el) {
        this[el.get("id")]();
    },

    moveLeft: function() {
        var next = (this.index == 0) ? this.elements.length-1 : this.index-1;
        this.swapFrames(next);
        this.fireEvent("left");
    },

    moveRight: function() {
        var next = (this.index < this.elements.length-1) ? this.index+1 : 0;
        this.swapFrames(next);
        this.fireEvent("right");
    },

    swapFrames: function(next) {
        // internal to do the swaps
        var curEl = this.elements[this.index];
        curEl.get("tween").removeEvents();
        curEl.set({
            "tween": {
                link: "cancel",
                onComplete: function() {
                    this.element.addClass("hide");
                }
            },
            styles: {
                opacity: 1
            }
        }).fade(0);

        this.index = next;

        var newEl = this.elements[this.index];
        newEl.get("tween").removeEvents();
        newEl.setStyle("opacity", 0).removeClass("hide").fade(1);
    }.protect(),

    stopRotation: function() {
        clearInterval(this.timer);
        this.controls.fade(1);
        this.fireEvent("stop");
    },

    startRotation: function() {
        clearInterval(this.timer);
        this.controls.fade(.5);
        this.timer = this.moveRight.periodical(this.options.delay, this);
        this.fireEvent("start");
    }
});


contentSwapper.Fancy = new Class({

    Extends: contentSwapper,

    initialize: function(element, options) {
        this.parent(element, options);
    },

    swapFrames: function(next) {
        var curEl = this.elements[this.index];
        curEl.get("morph").removeEvents();
        curEl.set({
            styles: {
                zIndex: 1000,
                opacity: 1
            },
            "morph": {
                link: "cancel",
                duration: 1000,
                onComplete: function() {
                    this.element.addClass("hide");
                }
            }
        }).morph({
            opacity: 0
        });

        this.index = next;

        var newEl = this.elements[this.index];
        newEl.get("morph").removeEvents();
        newEl.removeClass("hide").setStyles({
            zIndex: 1001,
            marginTop: -280,
            opacity: 0
        }).morph({
            marginTop: 0,
            opacity: 1
        });
    }

});

new contentSwapper.Fancy(document.id("rotator"));

the html:

<section class="rotator" id="rotator">
    <div class="pane" style="background-image:url(http://fragged.org/img/home/hunting-home.jpg)">
        <div class="info">
            <h2>Hunting CS style</h2>
            Built for your `camping` pleasure. nOOb
        </div>
    </div>
    <div class="pane hide" style="background-image:url(http://fragged.org/img/home/fishing-home.jpg)">
        <div class="info">
            <h2>Fishing? Really? </h2>
            Fishing is for mongs. <a href="#">Clcik here</a>
        </div>
    </div>
    <div class="pane hide" style="background-image:url(http://fragged.org/img/home/tourism-home.jpg)">
       <div class="info rambling">
           <h2>Rambling and walking</h2>
           Wish you were here? Can't blame you, it's lame.
        </div>
    </div>
</section>

etc etc. have fun

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