doulin1867 2010-12-22 18:46
浏览 54
已采纳

如何通过javascript滑块实时更改CSS颜色值?

I'm making a page where the user gets a javascript slider that goes from 0 to 100 and can use it to set the opacity of a div on the page.

I want the opacity of that div to change in real-time as they work the slider. I've not done this before. What's the best approach?

There cursor in the slider displays the slider's current value as you move it. It seems to be that I just need to find a way to display that value in any arbitrary other place on the page so I can display it in the style settings for the div.

The .js file that generates the slider has a line that (I think) is setting the current value in the cursor:

$(this).children(".ui-slider-handle", context).html(parseInt(settings[index]['default']));

TO get this changing number to display somewhere else at the same time, do I just need to add a div somewhere and then add a line like this?

$("#newDivId").children(".ui-slider-handle", context).html(parseInt(settings[index]['default']));

That seems like it would give me the number showing up in a div. How then would I get it into a form I could put into the style settings for a div?

If this was a php variable, I would do something like this,

style="opacity:<?php print $value ?>;"

What would be the .js equivalent?

  • 写回答

1条回答 默认 最新

  • duanrui3480 2010-12-22 18:51
    关注

    The jQuery UI Documentation has an example of a colorpicker with the slider widget. You should be able to grok it and adapt it for opacity.

    Here is a working example that I threw together using the color picker example as a base: http://jsfiddle.net/6VgzT/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100