将JQuery动画绑定到现有函数
我有一个现有的功能,当点击导航菜单项时,垂直站点的背景图像会发生变化将JQuery动画绑定到现有函数,jquery,html,css,background,Jquery,Html,Css,Background,我有一个现有的功能,当点击导航菜单项时,垂直站点的背景图像会发生变化 <script type="text/javascript"> $(function() { $('a.panel-home').click(function(){ $("#bgimg").attr('src',"<?php bloginfo('stylesheet_directory'); ?>/images/bg1.jpg"); return false; }); }
<script type="text/javascript">
$(function() {
$('a.panel-home').click(function(){
$("#bgimg").attr('src',"<?php bloginfo('stylesheet_directory'); ?>/images/bg1.jpg");
return false;
});
});
…但我对javascript/jquery有点陌生。将图像括在
div
中,然后将效果应用到div
,如下所示:
HTML:<div><img id="bgimg" src="...."></div>
JAVASCRIPT:
$(function() {
$('a.panel-home').click(function(){
$("#bgimg").parent().fadeOut(1000, function() {
$("#bgimg").attr('src',"<?php bloginfo('stylesheet_directory'); ?>/images/bg1.jpg");
$(this).fadeIn(1000);
});
return false;
});
});
$(函数(){
$('a.panel-home')。单击(函数(){
$(“#bgimg”).parent().fadeOut(1000,function()){
$(“#bgimg”).attr('src',“/images/bg1.jpg”);
美元(本).fadeIn(1000);
});
返回false;
});
});
要同时淡入淡出,可以使用以下代码:
/* This must be called on 'img' tag. Enhanced to support more than one image, first image in options argument,
* rest are in more_images argument, which is string array with following format:
* ["400|1000|apple.png","800|apple2.png", "apple3.png"]
* with 3 elements in pipe separation they are delay|duration|image
* with 2 elements they are duration|image
* and with one element, that's image, the default delay is zero and default duration is 800
*
* Ignore more_images argument if you only want to transition from one image to another one.
* Any external code can check if there's an animation running by checking if the parent div of the img
* being animated has the attribute data-anim . This is useful for external code to check
* if the transitions have ended (in an setinterval call for example)*/
jQuery.fn.transictionto = function(options, more_images) {
var settings = jQuery.extend({
}, options || {});
//wrap into div if no div is present.
$(this).each(function() {
if ($(this).parent('div').length == 0) {
$(this).wrap('<div></div>')
}
//mark to check if animation is in being done
$(this).parent().attr('data-anim','1');
//now swap with background trick
$(this)
.parent()
.css('background-image', 'url(' + settings.destinationImage + ')')
.css('background-repeat', 'no-repeat')
.end()
.fadeOut(settings.duration, function() {
this.src = settings.destinationImage;
$(this).show();
if (more_images != null && more_images.length) {
var im = more_images.shift().split('|');//shift shrinks array
var dly = im.length == 3 ? parseInt(im[0],10) : 0;
var drt = im.length > 1 ? parseInt(im[im.length-2],10) : 800;
$(this).delay(dly).transictionto({ destinationImage: im[im.length-1], duration: drt }, more_images);
} else {
$(this).parent().removeAttr('data-anim');
}
});
});
}
$(function() {
$('a.panel-home').click(function(){
destImage = "<?php bloginfo('stylesheet_directory'); ?>/images/bg1.jpg";
$("#bgimg").transictionto({ destinationImage: destImage, duration: 800 });
return false;
});
});
/*这必须在“img”标记上调用。增强以支持多个图像,选项参数中的第一个图像,
*其余部分位于more_images参数中,该参数为字符串数组,格式如下:
*[“400 | 1000 | apple.png”、“800 | apple2.png”、“apple3.png”]
*管道分离中有3个元素,它们是延迟|持续时间|图像
*有两个元素,它们是持续时间|图像
*对于一个元素,即图像,默认延迟为零,默认持续时间为800
*
*如果只想从一个图像过渡到另一个图像,请忽略“更多图像”参数。
*任何外部代码都可以通过检查img的父div
*正在设置动画的具有属性数据anim。这对于外部代码检查很有用
*如果转换已结束(例如在setinterval调用中)*/
jQuery.fn.TransactionTo=函数(选项,更多图片){
var settings=jQuery.extend({
},选项|{});
//如果不存在div,则将其包装到div中。
$(this).each(function(){
if($(this).parent('div').length==0){
$(此).wrap(“”)
}
//标记以检查动画是否正在完成
$(this.parent().attr('data-anim','1');
//现在交换背景技巧
$(本)
.parent()
.css('background-image','url('+settings.destinationImage+'))
.css('background-repeat'、'no-repeat')
(完)
.fadeOut(settings.duration,function(){
this.src=settings.destinationImage;
$(this.show();
if(more_images!=null&&more_images.length){
var im=more_images.shift().split(“|”);//shift收缩数组
var dly=im.length==3?parseInt(im[0],10):0;
var drt=im.length>1?parseInt(im[im.length-2],10):800;
$(this.delay(dly).transitionto({destinationImage:im[im.length-1],duration:drt},更多图像);
}否则{
$(this.parent().removeAttr('data-anim');
}
});
});
}
$(函数(){
$('a.panel-home')。单击(函数(){
desimage=“/images/bg1.jpg”;
$(“#bgimg”)。转换到({destinationImage:destinmage,duration:800});
返回false;
});
});
这次必须在
img
元素上调用transactionto()
函数。再次感谢……您知道同时淡入/淡出两个图像的方法吗?如果可能的话,我想尽量减少图片之间的背景色。谢谢你的努力。我将在本周晚些时候回到这个项目上,我将对此进行测试。
/* This must be called on 'img' tag. Enhanced to support more than one image, first image in options argument,
* rest are in more_images argument, which is string array with following format:
* ["400|1000|apple.png","800|apple2.png", "apple3.png"]
* with 3 elements in pipe separation they are delay|duration|image
* with 2 elements they are duration|image
* and with one element, that's image, the default delay is zero and default duration is 800
*
* Ignore more_images argument if you only want to transition from one image to another one.
* Any external code can check if there's an animation running by checking if the parent div of the img
* being animated has the attribute data-anim . This is useful for external code to check
* if the transitions have ended (in an setinterval call for example)*/
jQuery.fn.transictionto = function(options, more_images) {
var settings = jQuery.extend({
}, options || {});
//wrap into div if no div is present.
$(this).each(function() {
if ($(this).parent('div').length == 0) {
$(this).wrap('<div></div>')
}
//mark to check if animation is in being done
$(this).parent().attr('data-anim','1');
//now swap with background trick
$(this)
.parent()
.css('background-image', 'url(' + settings.destinationImage + ')')
.css('background-repeat', 'no-repeat')
.end()
.fadeOut(settings.duration, function() {
this.src = settings.destinationImage;
$(this).show();
if (more_images != null && more_images.length) {
var im = more_images.shift().split('|');//shift shrinks array
var dly = im.length == 3 ? parseInt(im[0],10) : 0;
var drt = im.length > 1 ? parseInt(im[im.length-2],10) : 800;
$(this).delay(dly).transictionto({ destinationImage: im[im.length-1], duration: drt }, more_images);
} else {
$(this).parent().removeAttr('data-anim');
}
});
});
}
$(function() {
$('a.panel-home').click(function(){
destImage = "<?php bloginfo('stylesheet_directory'); ?>/images/bg1.jpg";
$("#bgimg").transictionto({ destinationImage: destImage, duration: 800 });
return false;
});
});