Setting opacity of html element using Javascript

文章源自:http://viralpatel.net/blogs/setting-opacity-of-html-element-using-javascript/

Setting opacity of html element using Javascript

You must have seen the blur effect of html elements like table, div where the opacity of these elements are slowly made to transparent or opaque. This can be acheived through javascript.

Internet Exlporer supports a CSS attribute called filter, wherein you can apply lot of different filter options available in browser. There are currently three categories of filters – Visual filters and Reveal/Blend Transition filters. Multiple filters can be applied to a selector to produce interesting results, with the order of application often playing an important role in the final visual result. Current filters only apply in a visual context, but the extensibility of the property could allow for other capabilities.

Hence following code can be used to set opacity of an html element in IE.

var opacityValue = 0.5; //make it 50% opaque
myElement.style.filter = "alpha(opacity=" + opacityValue*100 + ")"; // IE

 But the above code will not work with other browsers like Firefox, Opera etc. Firefox supports another CSS property called opacity which controls the opacity of an element.
Following javascript function will set the opacity of any html element irrespective of the browser.

function setOpacity (myElement, opacityValue) {
    if (window.ActiveXObject) {
        myElement.style.filter = "alpha(opacity="
             + opacityValue*100 + ")"; // IE
    } else {
        myElement.style.opacity = opacityValue; // Gecko/Opera
    }
}

 

 

 

var options = { // // Zoomable waveform view options // zoomview: { // Container <div> element for the zoomable waveform view container: document.getElementById('zoomview-container'), // Color for the zoomable waveform // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0, 225, 128, 1)', // Color for the played region of the zoomable waveform // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Width of the playhead (pixels) playheadWidth: 1, // Tolerance for clicks in the zoomview to be interpreted as // dragging the playhead (pixels) playheadClickTolerance: 3, // Returns a string for the playhead timestamp label formatPlayheadTime: function, // Show current time next to the playhead showPlayheadTime: false, // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Returns a string for the axis label timestamps formatAxisTime: function, // Show or hide the axis label timestamps showAxisLabels: true, // Height of the axis markers at the top of the waveform axisTopMarkerHeight: 10, // Height of the axis markers at the top of the waveform axisBottomMarkerHeight: 10, // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Mouse-wheel mode: either 'none' or 'scroll' wheelMode: 'none', // Auto-scroll the waveform when the playhead reaches the edge of // the visible waveform autoScroll: true, // The offset in pixels edge of the visible waveform to trigger // auto-scroll autoScrollOffset: 100, // Enables point markers to be shown on the zoomable waveform enablePoints: true, // Enables segments to be shown on the zoomable waveform enableSegments: true, segmentOptions: { // Some segment options can be overridden for the zoomable waveform, // see segmentOptions below } }, // // Overview waveform options // overview: { // Container <div> element for the non-zoomable "overview" waveform container: document.getElementById('overview-container') // Color for the overview waveform // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0,0,0,0.2)', // Color for the played region of the overview waveform // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color for the overview waveform rectangle // that shows what the zoomable view shows highlightColor: 'grey', // Stroke color for the zoomed region highlightStrokeColor: 'transparent', // Opacity for the zoomed region highlightOpacity: 0.3, // Corner Radius for the zoomed region highlightCornerRadius: 2, // The default number of pixels from the top and bottom of the canvas // that the overviewHighlight takes up highlightOffset: 11, // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Returns a string for the playhead timestamp label formatPlayheadTime: function, // Show current time next to the playhead showPlayheadTime: false, // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Returns a string for the axis label timestamps formatAxisTime: function, // Show or hide the axis label timestamps showAxisLabels: true, // Height of the axis markers at the top of the waveform axisTopMarkerHeight: 10, // Height of the axis markers at the top of the waveform axisBottomMarkerHeight: 10, // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Enables point markers to be shown on the overview waveform enablePoints: true, // Enables segments to be shown on the overview waveform enableSegments: true, segmentOptions: { // Some segment options can be overridden for the overview waveform, // see segmentOptions below } }, // // Scrollbar options // scrollbar: { // Container <div> element for the scrollbar container: document.getElementById('scrollbar-container'), // Scrollbar color. The background color can be set using CSS on the // scrollbar container element color: '#888888', // Minimum scrollbar handle width, in pixels minWidth: 50 } // HTML media element containing an audio track mediaElement: document.querySelector('audio'), // // Pre-computed waveform data options // dataUri: { // Binary format waveform data URL arraybuffer: '/data/sample.dat', // JSON format waveform data URL json: '/data/sample.json', }, waveformData: { // ArrayBuffer containing binary format waveform data arraybuffer: null, // Object containing JSON format waveform data json: null }, // If true, Peaks.js will send credentials with all network requests, // i.e., when fetching waveform data withCredentials: false, // // Web Audio generated waveform data options // webAudio: { // A Web Audio AudioContext instance which can be used // to render the waveform if dataUri is not provided audioContext: new AudioContext(), // Alternatively, provide an AudioBuffer containing the decoded audio // samples. In this case, an AudioContext is not needed audioBuffer: null, // If true, the waveform will show all available channels // If false, the audio is shown as a single channel waveform multiChannel: false }, // Array of zoom levels in samples per pixel. Smaller numbers represent // being more "zoomed in". zoomLevels: [512, 1024, 2048, 4096], // To avoid computation when changing zoom level, Peaks.js maintains a cache // of waveforms at different zoom levels. This is enabled by default, but // can be disabled by setting waveformCache to false waveformCache: true // // Keyboard input options // // Bind keyboard controls keyboard: false, // Keyboard nudge increment in seconds (left arrow/right arrow) nudgeIncrement: 0.01, // // Default view options. Each of these can be set independently for each // waveform view, under the 'zoomview' and 'overview' options // (described above). // // Waveform color // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0, 225, 128, 1)', // Color for the played waveform region // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Show current time next to the playhead (zoomview only) showPlayheadTime: false, // // Point and segment options // // Default point marker color pointMarkerColor: '#ff0000', // if true, emit cue events on the Peaks instance (see Cue Events) emitCueEvents: false, // Enables point markers to be shown on the waveform views enablePoints: true, // Enables segments to be shown on the waveform views enableSegments: true, segmentOptions: { // Enable segment markers markers: true, // Enable segment overlays overlay: false, // Color for segment start marker handles startMarkerColor: '#aaaaaa', // Color for segment end marker handles endMarkerColor: '#aaaaaa', // Segment waveform color waveformColor: '#ff851b', // Segment overlay color overlayColor: '#ff0000', // Segment overlay opacity overlayOpacity: 0.3, // Segment overlay border color overlayBorderColor: '#ff0000', // Segment overlay border width overlayBorderWidth: 2, // Segment overlay border corner radius overlayCornerRadius: 5, // Segment overlay offset from the top and bottom of the waveform view, in pixels overlayOffset: 25, // Segment overlay label alignment, either 'top-left' or 'center' overlayLabelAlign: 'top-left', // Segment overlay label offset, in pixels overlayLabelPadding: 8, // Segment overlay label color overlayLabelColor: '#000000', // Segment overlay font family overlayFontFamily: 'sans-serif', // Segment overlay font size overlayFontSize: 12, // Segment overlay font style overlayFontStyle: 'normal' }, // // Customization options (see customizing.md) // createSegmentMarker: null, createSegmentLabel: null, createPointMarker: null, player: null, // // Point and segment initialization // segments: [ { startTime: 120, endTime: 140, editable: true, color: "#ff0000", labelText: "My label" }, { startTime: 220, endTime: 240, editable: false, color: "#00ff00", labelText: "My Second label" } ], points: [ { time: 150, editable: true, color: "#00ff00", labelText: "A point" }, { time: 160, editable: true, color: "#00ff00", labelText: "Another point" } ], // // Debugging options // // Diagnostic or error information is written to this function. // The default is console.error logger: console.error.bind(console) }; 里面是Peaks.js里的option各项配置,请翻一下各自有什么用处
最新发布
08-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值