首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >鼠标悬停在选框上的jQuery停止和开始不做

鼠标悬停在选框上的jQuery停止和开始不做
EN

Stack Overflow用户
提问于 2014-02-12 12:44:37
回答 7查看 11.5K关注 0票数 0

在一个简单的html标记中,我有一个marquee标签。因为我的代码是这样的

代码语言:javascript
复制
<marquee direction="left" scrolldelay="4" scrollamount="2" behavior="scroll">
    <a href="#">iPod Nano</a>
    <a href="#">iPod shuffle</a>
    <a href="#">MacBook</a>
    <a href="#">iPod touch</a>
</marquee>

在这里,我做了一个jQuery来停止悬停字幕,并再次做滚动字幕时,我将从字幕上删除悬停。为此,我的jQuery代码如下所示

代码语言:javascript
复制
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("marquee").hover(function(){
            this.stop();

        }, function() {
            this.start();
        });
    });
</script>

但这不是在鼠标悬停和离开时停止和启动。当我检查firebug控制台选项卡中的错误时,得到的错误信息如下

代码语言:javascript
复制
TypeError: this.start is not a function
TypeError: this.stop is not a function

有人能告诉我这是怎么回事吗?任何帮助和建议都将是非常有用的。谢谢

EN

回答 7

Stack Overflow用户

发布于 2014-02-12 13:02:00

通过使用内联onmouseover="this.stop();"onmouseout="this.start();,可以使用这种简单的方法来完成此操作

代码语言:javascript
复制
<marquee onmouseover="this.stop();" onmouseout="this.start();" direction="left" scrolldelay="4" scrollamount="2" behavior="scroll">
    <a href="#">iPod Nano</a>
    <a href="#">iPod shuffle</a>
    <a href="#">MacBook</a>
    <a href="#">iPod touch</a>
</marquee>

这是一个有效的。

希望能对你有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2014-02-12 12:47:25

尝试使用$(this)而不是this。显然,jquery中甚至没有start()方法。

另外,不要使用marquee,因为它已被弃用!

票数 0
EN

Stack Overflow用户

发布于 2014-03-08 23:42:24

这似乎可以使用JQuery,希望这能对你有所帮助。

代码语言:javascript
复制
<div class="demo">
    <marquee style="position: relative;" behavior="scroll" align="center" direction="up"      scrollamount="2" scrolldelay="5"  height="290"><br>
        <a href="#"> &bull; Rohir Naik  </a> <br><br>
        <a href="#"> &bull; Rohidas Naik  </a> <br><br>
        <a href="#"> &bull; Rohita Naik  </a> <br><br>
        <a href="#"> &bull; Omkar Naik  </a> <br><br>
    </marquee>
</div>

和要使用的脚本

代码语言:javascript
复制
(function ($) {
$.fn.marquee = function (klass) {
    var newMarquee = [],
        last = this.length;

    // works out the left or right hand reset position, based on scroll
    // behavior, current direction and new direction
    function getReset(newDir, marqueeRedux, marqueeState) {
        var behavior = marqueeState.behavior, width = marqueeState.width, dir = marqueeState.dir;
        var r = 0;
        if (behavior == 'alternate') {
            r = newDir == 1 ? marqueeRedux[marqueeState.widthAxis] - (width*2) : width;
        } else if (behavior == 'slide') {
            if (newDir == -1) {
                r = dir == -1 ? marqueeRedux[marqueeState.widthAxis] : width;
            } else {
                r = dir == -1 ? marqueeRedux[marqueeState.widthAxis] - (width*2) : 0;
            }
        } else {
            r = newDir == -1 ? marqueeRedux[marqueeState.widthAxis] : 0;
        }
        return r;
    }

    // single "thread" animation
    function animateMarquee() {
        var i = newMarquee.length,
            marqueeRedux = null,
            $marqueeRedux = null,
            marqueeState = {},
            newMarqueeList = [],
            hitedge = false;

        while (i--) {
            marqueeRedux = newMarquee[i];
            $marqueeRedux = $(marqueeRedux);
            marqueeState = $marqueeRedux.data('marqueeState');

            if ($marqueeRedux.data('paused') !== true) {
                // TODO read scrollamount, dir, behavior, loops and last from data
                marqueeRedux[marqueeState.axis] += (marqueeState.scrollamount * marqueeState.dir);

                // only true if it's hit the end
                hitedge = marqueeState.dir == -1 ? marqueeRedux[marqueeState.axis] <= getReset(marqueeState.dir * -1, marqueeRedux, marqueeState) : marqueeRedux[marqueeState.axis] >= getReset(marqueeState.dir * -1, marqueeRedux, marqueeState);

                if ((marqueeState.behavior == 'scroll' && marqueeState.last == marqueeRedux[marqueeState.axis]) || (marqueeState.behavior == 'alternate' && hitedge && marqueeState.last != -1) || (marqueeState.behavior == 'slide' && hitedge && marqueeState.last != -1)) {                        
                    if (marqueeState.behavior == 'alternate') {
                        marqueeState.dir *= -1; // flip
                    }
                    marqueeState.last = -1;

                    $marqueeRedux.trigger('stop');

                    marqueeState.loops--;
                    if (marqueeState.loops === 0) {
                        if (marqueeState.behavior != 'slide') {
                            marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
                        } else {
                            // corrects the position
                            marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir * -1, marqueeRedux, marqueeState);
                        }

                        $marqueeRedux.trigger('end');
                    } else {
                        // keep this marquee going
                        newMarqueeList.push(marqueeRedux);
                        $marqueeRedux.trigger('start');
                        marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
                    }
                } else {
                    newMarqueeList.push(marqueeRedux);
                }
                marqueeState.last = marqueeRedux[marqueeState.axis];

                // store updated state only if we ran an animation
                $marqueeRedux.data('marqueeState', marqueeState);
            } else {
                // even though it's paused, keep it in the list
                newMarqueeList.push(marqueeRedux);                    
            }
        }

        newMarquee = newMarqueeList;

        if (newMarquee.length) {
            setTimeout(animateMarquee, 25);
        }            
    }

    // TODO consider whether using .html() in the wrapping process could lead to loosing predefined events...
    this.each(function (i) {
        var $marquee = $(this),
            width = $marquee.attr('width') || $marquee.width(),
            height = $marquee.attr('height') || $marquee.height(),
            $marqueeRedux = $marquee.after('<div ' + (klass ? 'class="' + klass + '" ' : '') + 'style="display: block-inline; width: ' + width + 'px; height: ' + height + 'px; overflow: hidden;"><div style="float: left; white-space: nowrap;">' + $marquee.html() + '</div></div>').next(),
            marqueeRedux = $marqueeRedux.get(0),
            hitedge = 0,
            direction = ($marquee.attr('direction') || 'left').toLowerCase(),
            marqueeState = {
                dir : /down|right/.test(direction) ? -1 : 1,
                axis : /left|right/.test(direction) ? 'scrollLeft' : 'scrollTop',
                widthAxis : /left|right/.test(direction) ? 'scrollWidth' : 'scrollHeight',
                last : -1,
                loops : $marquee.attr('loop') || -1,
                scrollamount : $marquee.attr('scrollamount') || this.scrollAmount || 2,
                behavior : ($marquee.attr('behavior') || 'scroll').toLowerCase(),
                width : /left|right/.test(direction) ? width : height
            };

        // corrects a bug in Firefox - the default loops for slide is -1
        if ($marquee.attr('loop') == -1 && marqueeState.behavior == 'slide') {
            marqueeState.loops = 1;
        }

        $marquee.remove();

        // add padding
        if (/left|right/.test(direction)) {
            $marqueeRedux.find('> div').css('padding', '0 ' + width + 'px');
        } else {
            $marqueeRedux.find('> div').css('padding', height + 'px 0');
        }

        // events
        $marqueeRedux.bind('stop', function () {
            $marqueeRedux.data('paused', true);
        }).bind('pause', function () {
            $marqueeRedux.data('paused', true);
        }).bind('start', function () {
            $marqueeRedux.data('paused', false);
        }).bind('unpause', function () {
            $marqueeRedux.data('paused', false);
        }).data('marqueeState', marqueeState); // finally: store the state

        // todo - rerender event allowing us to do an ajax hit and redraw the marquee

        newMarquee.push(marqueeRedux);

        marqueeRedux[marqueeState.axis] = getReset(marqueeState.dir, marqueeRedux, marqueeState);
        $marqueeRedux.trigger('start');

        // on the very last marquee, trigger the animation
        if (i+1 == last) {
            animateMarquee();
        }
    });            

    return $(newMarquee);
};
}(jQuery));

<!--
$(function () {
    // basic version is: $('div.demo marquee').marquee() - but we're doing some sexy extras

    $('div.demo marquee').marquee('pointer').mouseover(function () {
        $(this).trigger('stop');
    }).mouseout(function () {
        $(this).trigger('start');
    }).mousemove(function (event) {
        if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
        }
    }).mousedown(function (event) {
        $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function () {
        $(this).data('drag', false);
    });
});
//-->

参考:http://remysharp.com/2008/09/10/the-silky-smooth-marquee/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21718876

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档