让div用鼠标打开而不是点击

发布于 2024-12-11 11:21:52 字数 274 浏览 0 评论 0原文

我做了这个: 这个 在右侧你会看到一个红色按钮。当你点击红色按钮时。带有文本的内容屏幕即将出现。但我对此有一个疑问。我可以用其他动画制作这个吗?如果你按住鼠标。然后就可以滑动打开了。将鼠标按钮向左移动。然后内容框打开。你明白吗?我希望你能帮助我。

你可以在jsfiddle上看到代码。您可以在那里更改它。我希望你能帮助我。我是一个初级 JavaScript 开发者。以及如何 不知道我该如何做到这一点。

I have make this: This In the right you see a red button. When you click on the red button. The content screen with the text is coming. But i have a question of this. Can i make this with a other animation. If you hold your mouse. Then you can slide open. With your mouse button to left. Then the content box open. Do you understand it? I hope you can help me.

You can see the code on jsfiddle. And you can change it there. I hope you can help me. I am a starting javascripter. And how And have no idea how I can make this.

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

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

发布评论

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

评论(3

梦途 2024-12-18 11:21:52

要实现拖动,您可以使用 mousedown/mouseup/mousemove,如下所示:http://jsfiddle.net/pimvdb/25y4K/8/

$(function () {
    "use strict";
    var box = $(".what-is-delicious"),
        button = $(".what-is-delicious > a");

    var mouseDown = false,
        grabbed   = 0,
        start     = -303;

    button.mousedown(function(e) {
        mouseDown = true;
        $('*').bind('selectstart', false); // prevent selections when dragging
        grabbed = e.pageX; // save where you grabbed
        $("body").append('<div class="background-overlay"></div>');
    });

    $('body').mouseup(function() {
        mouseDown = false;
        $('*').unbind('selectstart', false); // allow selections again
        $(".background-overlay").remove();
        start = parseInt(box.css('right'), 10); // save start for next time
                                                // (parseInt to remove 'px')

    }).mousemove(function (e) {
        if(mouseDown) { // only if you are dragging
            // set right to grabbed - pageX (difference) + start 'right' when started
            // dragging. And if you drag too far, set it to 0.
            box.css("right", Math.min(grabbed - e.pageX + start, 0));
        }
    });
});

To implement dragging, you can make use of mousedown/mouseup/mousemove like this: http://jsfiddle.net/pimvdb/25y4K/8/.

$(function () {
    "use strict";
    var box = $(".what-is-delicious"),
        button = $(".what-is-delicious > a");

    var mouseDown = false,
        grabbed   = 0,
        start     = -303;

    button.mousedown(function(e) {
        mouseDown = true;
        $('*').bind('selectstart', false); // prevent selections when dragging
        grabbed = e.pageX; // save where you grabbed
        $("body").append('<div class="background-overlay"></div>');
    });

    $('body').mouseup(function() {
        mouseDown = false;
        $('*').unbind('selectstart', false); // allow selections again
        $(".background-overlay").remove();
        start = parseInt(box.css('right'), 10); // save start for next time
                                                // (parseInt to remove 'px')

    }).mousemove(function (e) {
        if(mouseDown) { // only if you are dragging
            // set right to grabbed - pageX (difference) + start 'right' when started
            // dragging. And if you drag too far, set it to 0.
            box.css("right", Math.min(grabbed - e.pageX + start, 0));
        }
    });
});
雨后彩虹 2024-12-18 11:21:52

这是一个更新的小提琴。 基本上我只是做了几件事:

  • 将处理程序从“click”更改为向“mouseenter”
  • 添加了一个“mouseleave”处理程序,它执行相反的操作
  • what-is-delicious”容器上,而不是

代码:

$(function () {
    "use strict"
    var box = $(".what-is-delicious"),
        button = $(".what-is-delicious > a");
    box.mouseenter(function (e) {
        e.preventDefault();
        if ($(button).hasClass("open")) {

        } else {
            $("body").append('<div class="background-overlay"></div>');
            button.addClass("open");
            box.animate({ right: "0"}, 750);
        }
    }).mouseleave(function (e) {
        e.preventDefault();
        if ($(button).hasClass("open")) {
            $("body").find('div.background-overlay').remove();
            button.removeClass("open");
            box.animate({ right: -303}, 750);

        } else {
        }
    });
});

将处理程序放在“ “preventDefault()”调用不再是真正必要的,但我把它们留在那里。

Here is an updated fiddle. Basically I just did a couple of things:

  • Changed the handler from "click" to "mouseenter"
  • Added a "mouseleave" handler that does the opposite thing
  • Put the handlers on the "what-is-delicious" container instead of the <a>

The code:

$(function () {
    "use strict"
    var box = $(".what-is-delicious"),
        button = $(".what-is-delicious > a");
    box.mouseenter(function (e) {
        e.preventDefault();
        if ($(button).hasClass("open")) {

        } else {
            $("body").append('<div class="background-overlay"></div>');
            button.addClass("open");
            box.animate({ right: "0"}, 750);
        }
    }).mouseleave(function (e) {
        e.preventDefault();
        if ($(button).hasClass("open")) {
            $("body").find('div.background-overlay').remove();
            button.removeClass("open");
            box.animate({ right: -303}, 750);

        } else {
        }
    });
});

The "preventDefault()" calls aren't really necessary anymore but I left them there.

摇划花蜜的午后 2024-12-18 11:21:52

我假设您正在 OnClick() 事件中当前切换 DIV 的 Style.Display。
可以从 Hover() 或 MouseOver() 调用相同的代码

I would assume you are toggling the Style.Display of the DIV currently in an OnClick() event.
The same code can be called from a Hover() or MouseOver()

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