【发布时间】:2016-01-08 07:07:04
【问题描述】:
两天前我开始使用/学习 html/css/jquery 来开发我的网站,它几乎准备好了,只有一个问题。
我使用 c.bavota 这个很棒的 jquery 插件来创建一个背景图像,它将调整到浏览器窗口的整个宽度/高度!
效果很好,但问题是我在背景上有其他图像(无法将它们附加/组合到一个背景图像中),当我缩放背景时会调整大小,但我的其他图像保持相同大小并位于它们的位置不对。
所以我的第一个想法是创建一个新函数,它也以同样的方式调整其他图像的大小。
我要编辑的函数……
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
$(document).ready(function() {
/**
* jQuery.fullBg
* Version 1.0
* Copyright (c) 2010 c.bavota - http://bavotasan.com
* Dual licensed under MIT and GPL.
* Date: 02/23/2010
**/
(function($) {
$.fn.fullBg = function(){
var bgImg = $(this);
function resizeImg() {
var imgwidth = bgImg.width();
var imgheight = bgImg.height();
var winwidth = $(window).width();
var winheight = $(window).height();
var widthratio = winwidth / imgwidth;
var heightratio = winheight / imgheight;
var widthdiff = heightratio * imgwidth;
var heightdiff = widthratio * imgheight;
if(heightdiff>winheight) {
bgImg.css({
width: winwidth+'px',
height: heightdiff+'px'
});
} else {
bgImg.css({
width: widthdiff+'px',
height: winheight+'px'
});
}
}
resizeImg();
$(window).resize(function() {
resizeImg();
});
};
})(jQuery)
});
$(window).load(function() {
$("#background").fullBg();
});
我曾尝试在我的非背景图像上使用此功能,但结果却太大了…… 我的猜测是如果我更换了 $(window).width();例如 $(Background).width();并调用 $("#imagetoberesized").fullBg(); 上的函数.height 也一样 它可能会起作用。但是我缺乏 jquery 技能来传递图像中的图像
<img src="imagepath/Main2.jpg" alt="" id="background"/>
<div class="Image">
<a href="#" class="tooltip">
<img src="imagepath/imagei.jpg" id="imagetoberesized"/>
<span>
<h3>Text</h3>
</span>
</a>
</div>
到函数来测试它。 那么有什么想法/如果它会起作用吗?和/或如何将我的图像传递给函数? 谢谢
【问题讨论】:
-
嗨@Cryptic 你所有的代码都在内容上,如果不是,请显示更多细节,谢谢
-
“你所有的代码都在内容上”你到底想问我什么?你想让我发布我所有的 index.html 吗?
-
真的吗??比如“背景”
-
它的背景图片!肯定会把它添加到 html sn-p 中。
-
我再举一个例子,希望能帮助你解决问题
标签: jquery jquery-plugins background image-resizing