Javascript jQuery脚本运行于(文档).ready和$(窗口).resize
我对javascript/jQuery一无所知 我有一个用jQuery(document)调用的脚本。准备好了,这很好,我还希望在调整窗口大小时脚本能够自我更新。脚本的调用方式如下:Javascript jQuery脚本运行于(文档).ready和$(窗口).resize,javascript,jquery,resize,ready,Javascript,Jquery,Resize,Ready,我对javascript/jQuery一无所知 我有一个用jQuery(document)调用的脚本。准备好了,这很好,我还希望在调整窗口大小时脚本能够自我更新。脚本的调用方式如下: jQuery(document).ready(function($) { $('#accordion').AccordionImageMenu({ 'openPc': <?PHP echo $params['openPc']; ?>, 'width':<?PHP
jQuery(document).ready(function($) {
$('#accordion').AccordionImageMenu({
'openPc': <?PHP echo $params['openPc']; ?>,
'width':<?PHP echo $params['width']; ?>,
'height':<?PHP echo $params['height']; ?>,
'effect': '<?PHP echo $params['effect']; ?>',
'duration': <?PHP echo $params['duration']; ?>,
'openItem': <?PHP echo ($params['keepMenuItemOpen'] ==1)? $openItem : "null"; ?>,
'border' : <?PHP echo $params['border']; ?>,
'color' : '<?PHP echo $params['color']; ?>',
'position' : '<?PHP echo $params['position']; ?>',
'fadeInTitle': <?PHP echo $params['fadeInTitle']; ?>
});
});
及
及
我想出来了。而不是添加$(窗口).resize(函数(){在调用页面中,我将其添加到包含的JS文件中。通过这种方式,我可以“看到”触发器并对其进行操作。为了对其进行操作,我创建了一个名为resize的新函数,该函数更新了我所需的参数。缺点:如果用鼠标手动调整浏览器窗口的大小,它会将菜单的大小一次调整1个像素,这并不完美,而且不会改变有时会摔倒。如果你只是简单地从一种尺寸换到另一种尺寸,那么效果就很好。这是完成的代码。我相信有更好的方法(提示),但我已经用尽了我自己的知识
(function( $ ){
function accordionMenuSetting(obj,settings) {
this.menuSettings = settings;
this.menuAnimate = animate;
this.menuResize = resize;
var _this = this;
function animate(obj,i){
$.each(obj, function(j) {
var menuWidth = $('#accordion-box').width();
//console.log ("width = "+$('#accordion-box').width());
//claculate initial openDim based on percentage of menuWidth
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
var neutralDim = Math.floor( (menuWidth- (_this.menuSettings.border*(obj.length-1)))/obj.length);
var remainder = menuWidth-(_this.menuSettings.border*(obj.length-1))- (neutralDim*obj.length);
var itemDim = closedDim;
if ( j == i ) {
itemDim = openDim;
}
if (typeof i == 'undefined') {
if (_this.menuSettings.openItem == null) { //keep active not set
if (j == (obj.length - 1)){ //if item is the last add the remainder to fill the space
itemDim = neutralDim + remainder;
}else{ //otherwise just use the neutral dimension
itemDim = neutralDim;
}
}
else if (_this.menuSettings.openItem == j) itemDim = openDim;
else itemDim = closedDim;
}
if (_this.menuSettings.position == 'vertical')
$(this).animate({'height':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
else
$(this).animate({'width':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
var title = $('span',this);
title.stop(true,false);
if (_this.menuSettings.fadeInTitle >0 && title.length > 0) {
if (itemDim == openDim) {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0.7});
else title.animate({'opacity':0});
} else {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0});
else title.animate({'opacity':0.7});
}
} else {
title.css("display", "none");
}
});
}
function resize(obj,i){
var defaultViewPort = 940;
var menuWidth = $('#accordion-box').width();
var menuScale = menuWidth / defaultViewPort;
console.log ("menuscale=" + menuScale);
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
_this.menuAnimate($this);
var maxDim = closedDim*$this.length + _this.menuSettings.border*$this.length + 10;
if (_this.menuSettings.position == 'vertical')
$(obj).css({'width':_this.menuSettings.width+'px','height':maxDim+'px'});
else
$(obj).css({'height':_this.menuSettings.height * menuScale+'px','width':menuWidth+'px'});
$.each($this, function(i) {
var aWidth = 'auto';
var aHeight = _this.menuSettings.height * menuScale+'px';
if (_this.menuSettings.position == 'vertical') {
aWidth = _this.menuSettings.width+'px';
aHeight = 'auto';
}
$(this).css({
'width': aWidth,
'height': aHeight
});
})
};
var $this = $('a',obj);
var defaultViewPort = 940;
var menuWidth = $('#accordion-box').width();
var menuScale = menuWidth / defaultViewPort;
console.log ("menuscale=" + menuScale);
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
_this.menuAnimate($this);
var maxDim = closedDim*$this.length + _this.menuSettings.border*$this.length + 10;
if (_this.menuSettings.position == 'vertical')
$(obj).css({'width':_this.menuSettings.width+'px','height':maxDim+'px'});
else
$(obj).css({'height':_this.menuSettings.height * menuScale+'px','width':menuWidth+'px'});
$.each($this, function(i) {
ImgSrc = $('img',this).attr('src');
$('img',this).hide();
var borderBottomValue = 0;
var borderRightValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
var aWidth = 'auto';
var aHeight = _this.menuSettings.height * menuScale+'px';
if (_this.menuSettings.position == 'vertical') {
borderBottomValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
borderRightValue = 0;
aWidth = _this.menuSettings.width+'px';
aHeight = 'auto';
}
if ( i == ($this.length-1)) {
borderBottomValue = 0;
borderRightValue = 0;
}
$(this).css({
'width': aWidth,
'height': aHeight,
'background-image':'url('+ImgSrc+')',
'background-size':'cover',
'background-color':_this.menuSettings.color,
'background-repeat':'no-repeat',
'border-bottom': borderBottomValue,
'border-right': borderRightValue
}).mouseenter(function() {
$this.stop(true,false);
_this.menuAnimate($this,i);
});
});
$(window).resize(function() {
_this.menuResize(obj);
});
$(obj).mouseleave(function() {
_this.menuAnimate($this);
});
}
$.fn.AccordionImageMenu = function( options ) {
var settings = {
'openPc': 44,
'width':200,
'height':200,
'effect': 'swing',
'duration': 400,
'openItem': null,
'border': 2,
'color':'#000000',
'position':'horizontal',
'fadeInTitle': true
};
return this.each(function() {
$(this).addClass("aim");
$('br',this).remove();
if ( options ) $.extend( settings, options );
var menu = new accordionMenuSetting(this,settings);
});
};
})( jQuery );
参数的宽度和高度在页面发送到客户端之前由php设置。一旦客户端接收到页面,所有动态代码都由javascript处理。感谢您的回复,所讨论的参数不是我需要动态更改的参数,下面是我设法修改以响应的脚本片段[code]var menuWidth=$('#accordion').width();//根据menuWidth var openDim=Math.round(menuWidth*(_this.menuSettings.openPc/100));[/code]我只需要脚本重新计算[code]var menuWidth=$('#accordion').width();[/code]从问题中不清楚你想做什么。调整手风琴的大小?如何调整?注释中的代码很难阅读。什么是
accordiomenusetting
在做什么?我已将完整脚本添加到原始帖子中供你查看。基本上,我需要在调整窗口大小时重新运行脚本,以便它可以重新缩放到手风琴(原始脚本是基于固定宽度的,我使用var menuWidth=$(“#accordion”).width()使其具有响应性;
$(function()
{$(window).resize(AccordionImageMenu).triggerHandler('resize'); });
jQuery(window).on('resize', function($){
$('#accordion').AccordionImageMenu({
'openPc': <?PHP echo $params['openPc']; ?>,
'width':<?PHP echo $params['width']; ?>,
'height':<?PHP echo $params['height']; ?>,
'effect': '<?PHP echo $params['effect']; ?>',
'duration': <?PHP echo $params['duration']; ?>,
'openItem': <?PHP echo ($params['keepMenuItemOpen'] ==1)? $openItem : "null"; ?>,
'border' : <?PHP echo $params['border']; ?>,
'color' : '<?PHP echo $params['color']; ?>',
'position' : '<?PHP echo $params['position']; ?>',
'fadeInTitle': <?PHP echo $params['fadeInTitle']; ?>
});
});
(function( $ ){
function accordionMenuSetting(obj,settings) {
this.menuSettings = settings;
this.menuAnimate = animate;
var _this = this;
function animate(obj,i){
$.each(obj, function(j) {
var menuWidth = $('#accordion').width();
//claculate initial openDim based on percentage of menuWidth
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
var neutralDim = Math.floor( (menuWidth- (_this.menuSettings.border*(obj.length-1)))/obj.length);
var remainder = menuWidth-(_this.menuSettings.border*(obj.length-1))- (neutralDim*obj.length);
var itemDim = closedDim;
if ( j == i ) {
itemDim = openDim;
}
if (typeof i == 'undefined') {
if (_this.menuSettings.openItem == null) { //keep active not set
if (j == (obj.length - 1)){ //if item is the last add the remainder to fill the space
itemDim = neutralDim + remainder;
}else{ //otherwise just use the neutral dimension
itemDim = neutralDim;
}
}
else if (_this.menuSettings.openItem == j) itemDim = openDim;
else itemDim = closedDim;
}
if (_this.menuSettings.position == 'vertical')
$(this).animate({'height':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
else
$(this).animate({'width':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
var title = $('span',this);
title.stop(true,false);
if (_this.menuSettings.fadeInTitle >0 && title.length > 0) {
if (itemDim == openDim) {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0.7});
else title.animate({'opacity':0});
} else {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0});
else title.animate({'opacity':0.7});
}
} else {
title.css("display", "none");
}
});
}
var $this = $('a',obj);
var defaultViewPort = 940;
var menuWidth = $('#accordion').width();
var menuScale = menuWidth / defaultViewPort;
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
_this.menuAnimate($this);
var maxDim = closedDim*$this.length + _this.menuSettings.border*$this.length + 10;
if (_this.menuSettings.position == 'vertical')
$(obj).css({'width':_this.menuSettings.width+'px','height':maxDim+'px'});
else
$(obj).css({'height':_this.menuSettings.height * menuScale+'px','width':menuWidth+'px'});
$.each($this, function(i) {
ImgSrc = $('img',this).attr('src');
$('img',this).hide();
var borderBottomValue = 0;
var borderRightValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
var aWidth = 'auto';
var aHeight = _this.menuSettings.height * menuScale+'px';
if (_this.menuSettings.position == 'vertical') {
borderBottomValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
borderRightValue = 0;
aWidth = _this.menuSettings.width+'px';
aHeight = 'auto';
}
if ( i == ($this.length-1)) {
borderBottomValue = 0;
borderRightValue = 0;
}
$(this).css({
'width': aWidth,
'height': aHeight,
'background-image':'url('+ImgSrc+')',
'background-size':'cover',
'background-color':_this.menuSettings.color,
'background-repeat':'no-repeat',
'border-bottom': borderBottomValue,
'border-right': borderRightValue
}).mouseenter(function() {
$this.stop(true,false);
_this.menuAnimate($this,i);
});
});
$(obj).mouseleave(function() {
_this.menuAnimate($this);
});
}
$.fn.AccordionImageMenu = function( options ) {
var settings = {
'openPc': 44,
'width':200,
'height':200,
'effect': 'swing',
'duration': 400,
'openItem': null,
'border': 2,
'color':'#000000',
'position':'horizontal',
'fadeInTitle': true
};
return this.each(function() {
$(this).addClass("aim");
$('br',this).remove();
if ( options ) $.extend( settings, options );
var menu = new accordionMenuSetting(this,settings);
});
};
})( jQuery );
(function( $ ){
function accordionMenuSetting(obj,settings) {
this.menuSettings = settings;
this.menuAnimate = animate;
this.menuResize = resize;
var _this = this;
function animate(obj,i){
$.each(obj, function(j) {
var menuWidth = $('#accordion-box').width();
//console.log ("width = "+$('#accordion-box').width());
//claculate initial openDim based on percentage of menuWidth
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
var neutralDim = Math.floor( (menuWidth- (_this.menuSettings.border*(obj.length-1)))/obj.length);
var remainder = menuWidth-(_this.menuSettings.border*(obj.length-1))- (neutralDim*obj.length);
var itemDim = closedDim;
if ( j == i ) {
itemDim = openDim;
}
if (typeof i == 'undefined') {
if (_this.menuSettings.openItem == null) { //keep active not set
if (j == (obj.length - 1)){ //if item is the last add the remainder to fill the space
itemDim = neutralDim + remainder;
}else{ //otherwise just use the neutral dimension
itemDim = neutralDim;
}
}
else if (_this.menuSettings.openItem == j) itemDim = openDim;
else itemDim = closedDim;
}
if (_this.menuSettings.position == 'vertical')
$(this).animate({'height':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
else
$(this).animate({'width':itemDim},_this.menuSettings.duration,_this.menuSettings.effect);
var title = $('span',this);
title.stop(true,false);
if (_this.menuSettings.fadeInTitle >0 && title.length > 0) {
if (itemDim == openDim) {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0.7});
else title.animate({'opacity':0});
} else {
if (_this.menuSettings.fadeInTitle ==2) title.animate({'opacity':0});
else title.animate({'opacity':0.7});
}
} else {
title.css("display", "none");
}
});
}
function resize(obj,i){
var defaultViewPort = 940;
var menuWidth = $('#accordion-box').width();
var menuScale = menuWidth / defaultViewPort;
console.log ("menuscale=" + menuScale);
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
_this.menuAnimate($this);
var maxDim = closedDim*$this.length + _this.menuSettings.border*$this.length + 10;
if (_this.menuSettings.position == 'vertical')
$(obj).css({'width':_this.menuSettings.width+'px','height':maxDim+'px'});
else
$(obj).css({'height':_this.menuSettings.height * menuScale+'px','width':menuWidth+'px'});
$.each($this, function(i) {
var aWidth = 'auto';
var aHeight = _this.menuSettings.height * menuScale+'px';
if (_this.menuSettings.position == 'vertical') {
aWidth = _this.menuSettings.width+'px';
aHeight = 'auto';
}
$(this).css({
'width': aWidth,
'height': aHeight
});
})
};
var $this = $('a',obj);
var defaultViewPort = 940;
var menuWidth = $('#accordion-box').width();
var menuScale = menuWidth / defaultViewPort;
console.log ("menuscale=" + menuScale);
var openDim = Math.round(menuWidth * (_this.menuSettings.openPc/100));
//calculate closedDim using initial openDim
var closedDim = Math.floor( ( menuWidth-openDim - (_this.menuSettings.border*(obj.length-1)))/(obj.length-1) );
//recalculate openDim based on closedDim. this ensures that we use the full width and are pixel perfect (-1 just to make sure)
var openDim = menuWidth-(_this.menuSettings.border*(obj.length-1))- (closedDim*(obj.length-1))-1;
_this.menuAnimate($this);
var maxDim = closedDim*$this.length + _this.menuSettings.border*$this.length + 10;
if (_this.menuSettings.position == 'vertical')
$(obj).css({'width':_this.menuSettings.width+'px','height':maxDim+'px'});
else
$(obj).css({'height':_this.menuSettings.height * menuScale+'px','width':menuWidth+'px'});
$.each($this, function(i) {
ImgSrc = $('img',this).attr('src');
$('img',this).hide();
var borderBottomValue = 0;
var borderRightValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
var aWidth = 'auto';
var aHeight = _this.menuSettings.height * menuScale+'px';
if (_this.menuSettings.position == 'vertical') {
borderBottomValue = 'solid '+_this.menuSettings.border+'px '+_this.menuSettings.color;
borderRightValue = 0;
aWidth = _this.menuSettings.width+'px';
aHeight = 'auto';
}
if ( i == ($this.length-1)) {
borderBottomValue = 0;
borderRightValue = 0;
}
$(this).css({
'width': aWidth,
'height': aHeight,
'background-image':'url('+ImgSrc+')',
'background-size':'cover',
'background-color':_this.menuSettings.color,
'background-repeat':'no-repeat',
'border-bottom': borderBottomValue,
'border-right': borderRightValue
}).mouseenter(function() {
$this.stop(true,false);
_this.menuAnimate($this,i);
});
});
$(window).resize(function() {
_this.menuResize(obj);
});
$(obj).mouseleave(function() {
_this.menuAnimate($this);
});
}
$.fn.AccordionImageMenu = function( options ) {
var settings = {
'openPc': 44,
'width':200,
'height':200,
'effect': 'swing',
'duration': 400,
'openItem': null,
'border': 2,
'color':'#000000',
'position':'horizontal',
'fadeInTitle': true
};
return this.each(function() {
$(this).addClass("aim");
$('br',this).remove();
if ( options ) $.extend( settings, options );
var menu = new accordionMenuSetting(this,settings);
});
};
})( jQuery );