jQuery UI 滑块 ->有鼠标滚轮支持吗?

发布于 2024-11-02 12:08:53 字数 1821 浏览 5 评论 0原文

你可能已经知道我是 jQuery 的新手,所以不属于这个主题的代码改进仍然是非常允许的。

这是我的 HTML 代码:

<div style="display: inline-block; width: 120px;">
    <div>
        Bananas:
        <br />
        <input id="bananas_amount" />
        <input id="bananas_amount_percent" />
    </div>
    <br />
    <div id="bananas" style="height:200px;"></div>
</div>

这是我可怕的 js 代码:(

$( "#bananas" ).slider({
    orientation: "vertical",
    range: "min",
    min: 0,
    max: 100,
    value: 20,
    step: 5,
    slide: function( event, ui ) {
        $( "#bananas_amount_percent" ).val( ui.value + " %" );

            // the code displays a percentage by standart, but I need the real value, too:
            var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
            var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
            var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
            $( "#bananas_amount" ).val( bananas_amount + " g" );
    }
});
$( "#bananas_amount_percent" ).val( $( "#bananas" ).slider( "value" ) + " %" );

// again the real value (else the value would not be updatet on reload-reset)
var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
$( "#bananas_amount" ).val( bananas_amount + " g" );

权重为 200)

但是,它正在工作,除了一个“小”细节:不使用鼠标滚轮! 我已经发现我需要这个扩展: https://github.com/brandonaaron/jquery- mousewheel/downloads

但我真的完全不知道如何将其实现到我的滑块(顺便说一句,我的网站上有 5 个)。

请帮忙, 谢谢!

as you may already know I'm new to jQuery, so Code-Improvements not belonging to this theme are still very allowed.

This is my HTML-Code:

<div style="display: inline-block; width: 120px;">
    <div>
        Bananas:
        <br />
        <input id="bananas_amount" />
        <input id="bananas_amount_percent" />
    </div>
    <br />
    <div id="bananas" style="height:200px;"></div>
</div>

And this is my horrifying js-code:

$( "#bananas" ).slider({
    orientation: "vertical",
    range: "min",
    min: 0,
    max: 100,
    value: 20,
    step: 5,
    slide: function( event, ui ) {
        $( "#bananas_amount_percent" ).val( ui.value + " %" );

            // the code displays a percentage by standart, but I need the real value, too:
            var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
            var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
            var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
            $( "#bananas_amount" ).val( bananas_amount + " g" );
    }
});
$( "#bananas_amount_percent" ).val( $( "#bananas" ).slider( "value" ) + " %" );

// again the real value (else the value would not be updatet on reload-reset)
var bananas_amount_percent = $( "#bananas_amount_percent" ).val();
var bananas_amount_percent = bananas_amount_percent.replace(" %", "");
var bananas_amount = Math.round((weight / 100) * bananas_amount_percent);
$( "#bananas_amount" ).val( bananas_amount + " g" );

(weight is 200)

However, it's working, except one "little" detail: not with the mousewheel!
I already found out that I need this extension: https://github.com/brandonaaron/jquery-mousewheel/downloads

But I've really absolutely no idea how to implement this to my Slider (I've 5 on my Site btw).

Help pls,
Thx!

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

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

发布评论

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

评论(3

就此别过 2024-11-09 12:08:53

鼠标滚轮插件对于它的作用来说太重了。我提取了精华。在所有浏览器中都能很好地工作。

$('#bananas').bind('mousewheel DOMMouseScroll', function (e) {
    var delta = 0, element = $(this), value, result, oe;
    oe = e.originalEvent; // for jQuery >=1.7
    value = element.slider('value');

    if (oe.wheelDelta) {
        delta = -oe.wheelDelta;
    }
    if (oe.detail) {
        delta = oe.detail * 40;
    }

    value -= delta / 8;
    if (value > 100) {
        value = 100;
    }
    if (value < 0) {
        value = 0;
    }

    result = element.slider('option', 'slide').call(element, e, { value: value });
    if (result !== false) {
        element.slider('value', value);
    }
    return false;
});

编辑:将 #slider 更改为 #bananas

编辑2:添加触发 slide 事件

因为您仅使用 value 属性我传递仅具有此属性的参数对象。如果您需要更多内容,请记住将其添加到鼠标滚轮代码中。

EDIT3:当 slide 函数返回 false 时添加更改取消功能(就像文档中一样)

更新:delta 不仅显示车轮方向,还显示还有更深层次的意义。它描述了要滚动的像素数。滚动的默认设置是 3 行,即 120 像素,但可以有所不同。
如果您只想检索车轮的方向,请将其更改

value -= delta / 8;

为:

value -= (delta > 0) ? 5 : (delta < 0) ? -5 : 0;

尽管 delta === 0 永远不会发生。

更新:为新版本的 jQuery 添加了部分(e.originalEvent)。

The mousewheel plugin is too heavy for its role. I extracted the essence. Works great in all browsers.

$('#bananas').bind('mousewheel DOMMouseScroll', function (e) {
    var delta = 0, element = $(this), value, result, oe;
    oe = e.originalEvent; // for jQuery >=1.7
    value = element.slider('value');

    if (oe.wheelDelta) {
        delta = -oe.wheelDelta;
    }
    if (oe.detail) {
        delta = oe.detail * 40;
    }

    value -= delta / 8;
    if (value > 100) {
        value = 100;
    }
    if (value < 0) {
        value = 0;
    }

    result = element.slider('option', 'slide').call(element, e, { value: value });
    if (result !== false) {
        element.slider('value', value);
    }
    return false;
});

EDIT: changed #slider to #bananas

EDIT2: added triggering slide event

Because you are using only value property I pass for parameter object with only this property. If you will need something more, remember to add it to the mousewheel code.

EDIT3: added change cancelling ability when slide function returns false (just like in the documentation)

UPDATE: delta shows not only the wheel direction, but also has a deeper meaning. It describes number of pixels to scroll. The default setting for scrolling is 3 lines which is 120 pixels, but it can differ.
If you want to retrieve only direction of wheel, change this:

value -= delta / 8;

to this:

value -= (delta > 0) ? 5 : (delta < 0) ? -5 : 0;

although delta === 0 should never occur.

UPDATE: Added part for newer versions of jQuery (e.originalEvent).

遮了一弯 2024-11-09 12:08:53

我以前没有使用过鼠标滚轮插件,但从我所看到的来看,它应该像这样使用:

$("#DivName").mousewheel(function(event, delta) {
     //Trigger slide event


      //Prevent the default mouse wheel
      event.preventDefault();

});

DivName 可能是你的滑块,我相信 Delta 是滚动方向(以单位为单位)所以我猜负数是向上,正数向下。

I haven't used the mousewheel plugin before but from what I've seen it should be used like this:

$("#DivName").mousewheel(function(event, delta) {
     //Trigger slide event


      //Prevent the default mouse wheel
      event.preventDefault();

});

With DivName probably being your slider and I believe Delta is the scroll direction (in units) So I would guess negative is up, positive down.

秉烛思 2024-11-09 12:08:53
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var slider = $('[your selector ]');
slider.lightSlider();
slider.on('mousewheel', function(e) {
    if (e.deltaY > 0) {
        slider.goToPrevSlide();
    } else {
        slider.goToNextSlide();
    }
    e.preventDefault();
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var slider = $('[your selector ]');
slider.lightSlider();
slider.on('mousewheel', function(e) {
    if (e.deltaY > 0) {
        slider.goToPrevSlide();
    } else {
        slider.goToNextSlide();
    }
    e.preventDefault();
});
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文