Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 扩大职能范围_Javascript_Jquery - Fatal编程技术网

Javascript 扩大职能范围

Javascript 扩大职能范围,javascript,jquery,Javascript,Jquery,我已经编写了一些jQuery函数来调整图像的大小,使它们的纵横比保持在pageload上,并调整窗口大小。这些函数工作得很好,但我专门为一个场景构建了它们。现在,出现了另一个类似的场景,在这个场景中,我必须执行几乎相同的功能,只使用iframe。为了成为一名更好的编码员,并且不添加额外的代码,我如何在整个站点以及$('.com background>img')场景中简洁高效地重新使用这些函数作为iFrame 以下是jQuery: function imageCalc() { $imgWi

我已经编写了一些jQuery函数来调整图像的大小,使它们的纵横比保持在pageload上,并调整窗口大小。这些函数工作得很好,但我专门为一个场景构建了它们。现在,出现了另一个类似的场景,在这个场景中,我必须执行几乎相同的功能,只使用iframe。为了成为一名更好的编码员,并且不添加额外的代码,我如何在整个站点以及
$('.com background>img')
场景中简洁高效地重新使用这些函数作为iFrame

以下是jQuery:

function imageCalc() {
    $imgWidth = $('.com-background > img').width();
    $imgHeight = $('.com-background > img').height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $('.com-background > img').css('margin-left', '-10px' );

}
function setImageDims() {
    $('.com-background > img').css('height', function( calcHeight ) { return $imgAspectRatio * $('#main-content').width(); });
    $('.com-background > img').css('width', function( calcWidth ) { return $('#main-content').width() + 20; }); 
}

function imageResize() {
    $('.com-background > img').css('width', function( resizeWidth ) { return $("body").width() });
    $('.com-background > img').css('height', function( resizeHeight ) { return $imgAspectRatio * $('.com-background > img').width();  });
}

评论中@nnnnnn的意思是:


更新函数以将选择器作为参数,而不是 硬编码

把这个翻过来:

function imageCalc() {
    $imgWidth = $('.com-background > img').width();
    $imgHeight = $('.com-background > img').height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $('.com-background > img').css('margin-left', '-10px' );

}
为此:

//where selector is your string query
function imageCalc(selector) {
    $imgWidth = $(selector).width();
    $imgHeight = $(selector).height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $(selector).css('margin-left', '-10px' );

}
上述更改将使您的函数具有通用性,即您可以对其他DOM对象执行相同的操作,只要通过选择器参数指定要更改的内容。要执行与现在相同的操作,必须调用
imageCalc('.com background>img'),等等

通过这个,

另外,与您的问题无关,请缓存jQuery对象,不要 继续在同一功能中重新选择相同的元素

他的意思是:

//where selector is your string query
function imageCalc(selector) {
    var obj=$(selector);
    $imgWidth = obj.width();
    $imgHeight = obj.height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    obj.css('margin-left', '-10px' );

}

这样做的目的是缓存对象,这意味着您只需定位它一次,然后重用同一个对象。这会影响性能,因为每次使用
$(选择器)
,您都在搜索DOM树以找到一个元素,这确实需要时间。

注释中的@nnnn意味着:


更新函数以将选择器作为参数,而不是 硬编码

把这个翻过来:

function imageCalc() {
    $imgWidth = $('.com-background > img').width();
    $imgHeight = $('.com-background > img').height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $('.com-background > img').css('margin-left', '-10px' );

}
为此:

//where selector is your string query
function imageCalc(selector) {
    $imgWidth = $(selector).width();
    $imgHeight = $(selector).height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $(selector).css('margin-left', '-10px' );

}
上述更改将使您的函数具有通用性,即您可以对其他DOM对象执行相同的操作,只要通过选择器参数指定要更改的内容。要执行与现在相同的操作,必须调用
imageCalc('.com background>img'),等等

通过这个,

另外,与您的问题无关,请缓存jQuery对象,不要 继续在同一功能中重新选择相同的元素

他的意思是:

//where selector is your string query
function imageCalc(selector) {
    var obj=$(selector);
    $imgWidth = obj.width();
    $imgHeight = obj.height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    obj.css('margin-left', '-10px' );

}

这样做的目的是缓存对象,这意味着您只需定位它一次,然后重用同一个对象。这会影响性能,因为每次使用
$(选择器)
,您都在搜索DOM树以找到一个元素,这确实需要时间。

注释中的@nnnn意味着:


更新函数以将选择器作为参数,而不是 硬编码

把这个翻过来:

function imageCalc() {
    $imgWidth = $('.com-background > img').width();
    $imgHeight = $('.com-background > img').height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $('.com-background > img').css('margin-left', '-10px' );

}
为此:

//where selector is your string query
function imageCalc(selector) {
    $imgWidth = $(selector).width();
    $imgHeight = $(selector).height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $(selector).css('margin-left', '-10px' );

}
上述更改将使您的函数具有通用性,即您可以对其他DOM对象执行相同的操作,只要通过选择器参数指定要更改的内容。要执行与现在相同的操作,必须调用
imageCalc('.com background>img'),等等

通过这个,

另外,与您的问题无关,请缓存jQuery对象,不要 继续在同一功能中重新选择相同的元素

他的意思是:

//where selector is your string query
function imageCalc(selector) {
    var obj=$(selector);
    $imgWidth = obj.width();
    $imgHeight = obj.height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    obj.css('margin-left', '-10px' );

}

这样做的目的是缓存对象,这意味着您只需定位它一次,然后重用同一个对象。这会影响性能,因为每次使用
$(选择器)
,您都在搜索DOM树以找到一个元素,这确实需要时间。

注释中的@nnnn意味着:


更新函数以将选择器作为参数,而不是 硬编码

把这个翻过来:

function imageCalc() {
    $imgWidth = $('.com-background > img').width();
    $imgHeight = $('.com-background > img').height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $('.com-background > img').css('margin-left', '-10px' );

}
为此:

//where selector is your string query
function imageCalc(selector) {
    $imgWidth = $(selector).width();
    $imgHeight = $(selector).height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    $(selector).css('margin-left', '-10px' );

}
上述更改将使您的函数具有通用性,即您可以对其他DOM对象执行相同的操作,只要通过选择器参数指定要更改的内容。要执行与现在相同的操作,必须调用
imageCalc('.com background>img'),等等

通过这个,

另外,与您的问题无关,请缓存jQuery对象,不要 继续在同一功能中重新选择相同的元素

他的意思是:

//where selector is your string query
function imageCalc(selector) {
    var obj=$(selector);
    $imgWidth = obj.width();
    $imgHeight = obj.height();
    $imgAspectRatio =  $imgHeight / $imgWidth;
    obj.css('margin-left', '-10px' );

}

这样做的目的是缓存对象,这意味着您只需定位它一次,然后重用同一个对象。这会对性能产生影响,因为每次使用
$(选择器)
,您都在搜索DOM树以找到一个元素,这确实需要时间。

更新函数以将选择器作为参数,而不是硬编码它?(另外,与您的问题无关,请缓存jQuery对象,不要在同一函数中不断重新选择相同的元素。)能否将代码结构作为下面的示例作为答案?请更新函数,以将选择器作为参数,而不是硬编码?(另外,与您的问题无关,请缓存jQuery对象,不要在同一函数中不断重新选择相同的元素。)能否将代码结构作为下面的示例作为答案?请更新函数,以将选择器作为参数,而不是硬编码?(另外,与您的问题无关,请缓存jQuery对象,不要在同一函数中不断重新选择相同的元素。)能否将代码结构作为下面的示例作为答案?请更新函数,以将选择器作为参数,而不是硬编码?(另外,与您的问题无关,请缓存jQuery对象,不要在同一个函数中不断重新选择相同的元素。)请您将代码结构作为下面的示例作为答案,好吗?+1。是的,这就是我的意思。克里斯,还要注意,所有变量都应该用
var
声明(就像这个答案中的
obj
变量),否则它们将是全局变量。+1。是的,这就是我的意思。克里斯,还要注意,所有变量都应该用
var
声明(就像这个答案中的
obj
变量),否则它们将是全局变量。+1