我怎样才能在“鼠标输入”和“鼠标离开时”中仅使用一次变量
How can I use variables only once with 'on mouseenter' and 'on mouseleave'?
我有多个不同大小的对象,我希望每个对象都显示额外的框on mouseenter
并隐藏on mouseleave
。我有一个 jquery 脚本可以完美地做到这一点,我唯一担心的是我重复变量两次,并且有些东西告诉我这可以在不重复自己的情况下完成。
问题是它们都强烈地基于$(this)
核心元素,所以我不能使变量全局化。我的猜测是,在调用on mouseenter
和on mouseleave
之前,我应该将它们放在元素容器函数中,但是语法方面我不知道该怎么做。但同样,我可能在这一点上大错特错。
代码如下:
$(document).ready(function() {
$('.box-options').hide();
var $boxModule = $('div.box');
$boxModule.on({
mouseenter: function() {
var $this = $(this), // repeat
$options = $this.find('div.options'), // repeat
$optionsBox = $this.find('div.options-box'); // repeat
var boxHeight = $this.height(), // repeat
boxWidth = $this.width(), // repeat
optionsBoxHeight = $optionsBox.outerHeight(); // repeat
if ( // statement referring to variables above }
else { // statement referring to variables above };
$options.fadeIn(200).addClass('shadow').css({"height" : boxHeight + optionsBoxHeight});
$optionsBox.delay(100).fadeIn(200).css({"top" : boxHeight}, 200);
},
mouseleave: function() {
var $this = $(this), // repeat
$options = $this.find('div.options'), // repeat
$optionsBox = $this.find('div.options-box'); // repeat
var boxHeight = $this.height(), // repeat
boxWidth = $this.width(), // repeat
optionsBoxHeight = $optionsBox.outerHeight(); // repeat
$optionsBox.hide().css({"top" : boxHeight});
$options.hide().removeClass('shadow').css({"height" : boxHeight}, 200);
}
});
});
显然,代码包含更多行,但重要的部分是标记为// repeat
的变量。有谁知道如何重新构建代码以使变量只编写一次?
:我更新了代码以更好地描述逻辑。为了清楚起见,每个页面上还有多个具有相同类、结构和大小的对象,唯一的区别是内容(文本(和 ID 号。
使用悬停函数,对于变量,在悬停事件之前声明它们,就像你对$boxModule
所做的那样。
叫
$( selector ).hover( handlerIn, handlerOut )
是以下的简写:
$( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );
我认为该代码中重复的解决方案是创建一个外部函数,以从作为参数传递的元素中获取一些信息。
例如:
function options($element) {
return $element.find('div.options');
}
基于$(this(的所有其他属性也是如此。
然后,您可以在事件处理程序中使用选项,如下所示:options($this).fadeIn(200)
希望这有助于清理您的代码。
您可以在事件外部声明 var,例如:
$('div.box').each(function(){
var $this = $(this),
$options = $this.find('div.options'),
$optionsBox = $this.find('div.options-box'),
boxHeight = $this.height();
$this.on({
mouseenter: function() {...}
mouseleave: function() {...}
});
});
如何创建一个返回可在每个处理程序中使用的object
的function
?
var calcVars = function(){
var $this = $(this),
$options = $this.find('div.options'),
$optionsBox = $this.find('div.options-box');
var boxHeight = $this.height(),
boxWidth = $this.width(),
optionsBoxHeight = $optionsBox.outerHeight();
return {
boxHeight: boxHeight,
//every other variable you need outside
}
$boxModule.on({
firstEvent: function() {
var object = calcVars.call(this);
object.calculatedProperty.doSomething();
//other code
},
secondEvent: function() {
var object = calcVars.call(this);
object.anotherCalculatedProperty.doSomething();
//other code
}
})
或者你可以做:
$boxModule.on("anEvent anotherEvent", function(event) {
/*
var declarations
*/
var $this = $(this),
//etc..
if(event.type == "anEvent"){
doStuff();
else if(event.type == "anotherEvent"){
doOtherStuff();
}
})
相关文章:
- 将函数的上下文应用于javascript变量
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 将PHP变量传递给jQuery时遇到问题
- 如何通过ajax刷新JSF填充的javascript变量
- 参数变量出现ngTable指令问题
- 通过javascript重定向html传递php变量
- 我怎样才能在“鼠标输入”和“鼠标离开时”中仅使用一次变量
- 鼠标上的补间变量不起作用
- 在按键和鼠标单击后将函数变量转换为全局变量
- 如何向Gmap3标记添加一些新变量,并在鼠标事件触发时检索它们
- jQuery如何使用变量返回鼠标位置
- 在Angular 2中,直到鼠标悬停,组件变量才会在视图中更新
- 我如何在鼠标悬停时获得元素ID并将其分配给变量
- 将鼠标位置存储为变量,并与触发事件的值进行比较
- 如何测试水平鼠标位置是否等于Javascript中的一个变量
- 如何在鼠标悬停时显示传递变量的警告
- html5 canvas - JavaScript-将鼠标X和Y位置存储在变量中,并在鼠标移动时不断更新它们
- 如何在鼠标单击时更改 php 变量
- 获取鼠标移动的全局变量并在论文.js中更新 onFrame 事件