Javascript 将Jquery图像悬停,单击并使用选项卡和标题显示灰度效果
我想我的图像得到黑白(灰度)后,我悬停他们将改变颜色。它已经成功了,但我想如果我单击图像,它们将改变颜色,并在我已经添加的div中显示内容。单击.dblclick和.toogke函数,但始终将图像更改为黑白。下面是代码Javascript 将Jquery图像悬停,单击并使用选项卡和标题显示灰度效果,javascript,jquery,tabs,click,caption,Javascript,Jquery,Tabs,Click,Caption,我想我的图像得到黑白(灰度)后,我悬停他们将改变颜色。它已经成功了,但我想如果我单击图像,它们将改变颜色,并在我已经添加的div中显示内容。单击.dblclick和.toogke函数,但始终将图像更改为黑白。下面是代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Tabbed Navigation</title>
<style type="text/css">
.item {
width: auto;
margin: 0 0 0 0;
float: left;
}
.item.first {
clear: left;
margin-left: 0;
}
.item img {
opacity:0;
}
.wrapper{
float:left; /* important */
position:relative; /* important(so we can absolutely position the description div */
margin-right: 20px
}
.description{
position:absolute; /* absolute position (so we can position it where we want)*/
bottom:50px; /* position will be on bottom */
width:100%;
/* styling bellow */
background-color:black;
font-family: 'tahoma';
font-size:15px;
color:white;
opacity:0.6; /* transparency */
filter:alpha(opacity=60); /* IE transparency */
z-index: 9999;
}
p.description_content{
padding:5px;
margin:0px;
}
.item img a:active {
background-image: url(images/icondock.jpg);
}
</style>
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
// $(".item img").css({"display":"none");
// 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").animate({opacity:1},500);
// clone image
$('.item img').each(function(){
var el = $(this);
el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").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}, 500);
})
$('.img_grayscale').mouseout(function(){
$(this).stop().animate({opacity:0}, 500);
});
});
// 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();
}
</script>
</head>
<body>
<div class='wrapper'>
<div class="item">
<a href="#11"><img src="images/icondock.jpg" /></a>
</div>
<div class="description">
<p class="description_content">The pack, the basic The pack, the basic <a href="#tab1">More »</a> </p>
</div>
</div>
<div class='wrapper'>
<div class="item">
<a href="#22"><img src="images/koifish.jpg" /></a>
</div>
<div class="description">
<p class="description_content">The pack, the basic The pack, the basic <a href="#tab2">More »</a> </p>
</div>
</div>
<div class='wrapper'>
<div class="item">
<a href="#33"><img src="images/sakura.jpg" /></a>
</div>
<div class="description">
<p class="description_content">The pack, the basic The pack, the basic <a href="#tab3">More »</a> </p>
</div>
</div>
</body>
</html>
选项卡式导航
.项目{
宽度:自动;
保证金:0;
浮动:左;
}
.第一项{
清除:左;
左边距:0;
}
.项目img{
不透明度:0;
}
.wrapper{
浮动:左;/*重要*/
position:relative;/*重要(因此我们可以绝对定位description div*/
右边距:20px
}
.说明{
位置:绝对;/*绝对位置(因此我们可以将其放置在需要的位置)*/
底部:50px;/*位置将位于底部*/
宽度:100%;
/*样式波纹管*/
背景色:黑色;
字体系列:“tahoma”;
字体大小:15px;
颜色:白色;
不透明度:0.6;/*透明度*/
过滤器:alpha(不透明度=60);/*即透明度*/
z指数:9999;
}
p、 说明内容{
填充物:5px;
边际:0px;
}
.项目img a:激活{
背景图片:url(images/icondock.jpg);
}
//$(“.item img”).css({“display”:“none”);
//窗口加载。这将等待图像加载完成,这是必不可少的
$(窗口)。加载(函数(){
//淡入图像,这样就不会加载彩色“弹出”文档,然后再加载窗口
$(“.item img”).animate({opacity:1},500);
//克隆图像
$('.item img')。每个(函数(){
var el=$(本);
css({“位置”:“绝对”}).wrap(“”.clone().addClass('img_grayscale').css({“位置”:“绝对”,“z索引”:“998”,“不透明度”:“0”}).insertBefore(el).queue(函数(){
var el=$(本);
el.parent().css({“width”:this.width,“height”:this.height});
el.dequeue();
});
this.src=灰度(this.src);
});
//淡入图像
$('.item img').mouseover(函数(){
$(this.parent().find('img:first').stop().animate({opacity:1},500);
})
$('.img_grayscale').mouseout(函数(){
$(this.stop().animate({opacity:0},500);
});
});
//灰度w画布法
函数灰度(src){
var canvas=document.createElement('canvas');
var ctx=canvas.getContext('2d');
var imgObj=新图像();
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);
对于(变量y=0;y包装、基本包装、基本包装
包装、基本包装、基本包装
包装、基本包装、基本包装
如果非常困难,您可以使用2个图像灰度和颜色javascript:
$(function(){
var canv = document.createElement('canvas'),
ctx = canv.getContext('2d');
canv.style.cssText = "position:absolute;left:-10000px;";
document.body.appendChild( canv );
function getGrayScale(src, el ){
$("<img src=\""+src+"\" />").appendTo("body").bind("load", function ( ) {
var w = canv.width = this.offsetWidth,
h = canv.height = this.offsetHeight;
ctx.drawImage( this, 0, 0);
var imgPixels = ctx.getImageData(0, 0, w, h);
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);
$("<img class = \"img_grayscale\" src=\""+ canv.toDataURL() +"\" alt=\"\" />")
.insertBefore(el)
$(this).remove();
})
}
$(".item img").each(function(){
getGrayScale(this.src, $(this));
})
$(".item").delegate( ".img_grayscale", "mouseenter", function ( ) {
$(this).stop().animate({opacity:0}, 500)
}).delegate( ".img_grayscale","mouseout", function(){
$(this).stop().animate({opacity:1}, 500);
}).delegate( ".img_grayscale","click", function(){
$(this).remove();
});
});
删除css:
.item img {opacity:0;}
我不知道出了什么问题,但这似乎像您预期的那样起作用。在
标记后
放置不当,请以适当的形式更新它。已编辑:)thx用于它,但仍然无效thx用于您的注释:)但它不起作用,,单击并取消覆盖图像后,再次转到bw:(如果太难,可以使用2幅灰度和彩色图像
.item img {opacity:0;}