【问题标题】:jQuery slider control for opacity用于不透明度的 jQuery 滑块控件
【发布时间】:2012-02-12 02:06:20
【问题描述】:

我正在尝试为我的网页上某个元素的不透明度实现一个 jQuery 滑块控件。

有点像this question,但有一个滑块。

我想知道我应该如何最好地实现这个,因为我有点迷茫我应该如何开始......

我猜一个函数不会是最好的,不是吗?定义一个函数,然后为滑块调用它?

事实证明,滑块控件的 jQuery documentation 对于这个主题对我来说有点过于复杂,但我相信你们中的一些人可以帮助澄清如何让这件事继续下去!

抱歉,这个问题有点含糊,但我不确定如何继续。

【问题讨论】:

    标签: jquery slider opacity


    【解决方案1】:

    我不确定您希望最终结果是什么,但这里有一个简单的滑块示例,用于控制页面上另一个元素的不透明度。我已经在我的 Javascript 中包含了相关部分的 cmets。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Slider</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.3/themes/base/jquery-ui.css" type="text/css"/>
    
        <script type="text/javascript">
            $(document).ready(function() {
                //Step 1: set up the slider with some options. The valid values for opacity are 0 to 1
                //Step 2: Bind an event so when you slide the slider and stop, the following function gets called
                $('#slider').slider({ min: 0, max: 1, step: 0.1, value: 1 })
                    .bind("slidechange", function() {
                        //get the value of the slider with this call
                        var o = $(this).slider('value');
                        //here I am just specifying the element to change with a "made up" attribute (but don't worry, this is in the HTML specs and supported by all browsers).
                        var e = '#' + $(this).attr('data-wjs-element');
                        $(e).css('opacity', o)
                    });
            });
        </script>
        <style type="text/css">
            #box { width: 200px; height: 200px; background-color: #ff0000; }
            #slider { width: 200px; }
        </style>
    </head>
    <body>
        <div id="slider" data-wjs-element="box"></div>
        <div id="box">
            <p>Fade with the above slider...</p>
        </div>
    </body>
    </html>
    

    【讨论】:

    • 非常感谢!您的代码解释得非常透彻,并且是一个很好的有用注释示例! :) 再次感谢。现在,我终于明白如何使用滑块了,绑定功能也很有帮助。
    【解决方案2】:

    这是一个很好的代码示例,但我稍微修改了上面的代码,滑块现在在开始滑动时立即改变不透明度,并且不透明度变化更加平滑。`

    滑块

    <script type="text/javascript">
        $(document).ready(function() {
            //Step 1: set up the slider with some options. The valid values for opacity are 0 to 1
            //Step 2: Bind an event so when you slide the slider and stop, the following function gets called
            $('#slider').slider({ 
            min: 0, 
            max: 1, 
            step: 0.01, 
            value: 1,
            orientation: "vertical",
                 slide: function(e,ui){
                         $('#box').css('opacity', ui.value)
    
                 }                
            })
        });
    </script>
    <style type="text/css">
        #box { width: 200px; height: 200px; background-color: #ff0000; }
        #slider { width: 15px; }
    </style>
    

    使用上面的滑块淡化...

    `

    【讨论】:

    • 感谢您的帮助,尽管问题已经得到解答! :)
    • 这段代码更好。 var o = $(this).slider('value'); 在您将滑块一直向左滑动时无法正常工作。使用function(e,ui){ var 0 = ui.value; } 将防止出现此错误。
    猜你喜欢
    • 2017-11-28
    • 2018-12-17
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2015-05-03
    • 2012-08-15
    • 2013-11-19
    • 2010-12-22
    相关资源
    最近更新 更多