html让图片悬浮在网页,网页制作灰度图片悬浮效果利用HTML5和jQuery实现

jQuery 代码:

// On window load. This waits until images have loaded which is essential

$(window).load(function(){

// Fade in images so there isn't a color "pop" document load and then on window load

$(".item img").fadeIn(500);

// clone image

$('.item img').each(function(){

var el = $(this);

el.css({"position":"absolute"}).wrap("

").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){

var el = $(this);

el.parent().css({"width":this.width,"height":this.height});

el.dequeue();

});

this.src = grayscale(this.src);

});

// Fade image

$('.item img').mouseover(function(){

$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);

})

$('.img_grayscale').mouseout(function(){

$(this).stop().animate({opacity:0}, 1000);

});

});

// Grayscale w canvas method

function grayscale(src){

var canvas = document.createElement('canvas');

var ctx = canvas.getContext('2d');

var imgObj = new Image();

imgObj.src = src;

canvas.width = imgObj.width;

canvas.height = imgObj.height;

ctx.drawImage(imgObj, 0, 0);

var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);

for(var y = 0; y < imgPixels.height; y++){

for(var x = 0; x < imgPixels.width; x++){

var i = (y * 4) * imgPixels.width + x * 4;

var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;

imgPixels.data[i] = avg;

imgPixels.data[i + 1] = avg;

imgPixels.data[i + 2] = avg;

}

}

ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);

return canvas.toDataURL();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的HTML5音乐播放器插件代码,可以固定在网页底部悬浮显示: ```html <div id="audio-player" style="position: fixed; bottom: 0; left: 0; width: 100%; background-color: #333; color: #fff; padding: 10px;"> <audio id="music" src="path/to/music.mp3"></audio> <button id="play-pause-btn">播放</button> <span id="current-time">0:00</span>/<span id="duration">0:00</span> <input id="volume-slider" type="range" min="0" max="1" step="0.1" value="1"> </div> <script> var music = document.getElementById("music"); var playPauseBtn = document.getElementById("play-pause-btn"); var currentTime = document.getElementById("current-time"); var duration = document.getElementById("duration"); var volumeSlider = document.getElementById("volume-slider"); function playPause() { if (music.paused) { music.play(); playPauseBtn.innerHTML = "暂停"; } else { music.pause(); playPauseBtn.innerHTML = "播放"; } } function updateTime() { var currentTimeValue = Math.floor(music.currentTime); var durationValue = Math.floor(music.duration); var minutes = Math.floor(currentTimeValue / 60); var seconds = currentTimeValue - minutes * 60; currentTime.innerHTML = minutes + ":" + (seconds < 10 ? "0" : "") + seconds; minutes = Math.floor(durationValue / 60); seconds = durationValue - minutes * 60; duration.innerHTML = minutes + ":" + (seconds < 10 ? "0" : "") + seconds; } function updateVolume() { music.volume = volumeSlider.value; } playPauseBtn.addEventListener("click", playPause); music.addEventListener("timeupdate", updateTime); volumeSlider.addEventListener("input", updateVolume); </script> ``` 你可以将 `path/to/music.mp3` 替换为你的音乐文件路径,然后将这段代码嵌入到你的网页中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值