为什么我的 Jquery 元素调整大小不起作用?
我在 FF、Safari 和 Chrome 中使用 CSS 的 resize 属性来调整 div 元素的大小,但由于此属性在 IE 和 Opera 中不起作用,我被迫找到备份,以防使用这两种浏览器之一,所以我使用 Jquery 和 javascript 的组合来询问我在网上找到的一个函数,这里: 浏览器调整大小支持代码 。不幸的是,我担心这段代码在我的测试中不起作用,看看它是否有效。
这是我的测试代码: 我的代码
var unsupported = (function()
{
var div = document.createElement('div'),
vendors = 'Khtml Ms O Moz Webkit'.split(' '),
len = vendors.length;
return function(prop)
{
if( prop in div.style ) return true;
prop = prop.replace(/^[a-z]/, function(val)
{
return val.toUpperCase();
});
while(len--)
{
if ( vendors[len] + prop in div.style )
{
return false;
}
}
return true;
};
})();
if( unsupported('resize') )
{
$("#elementtoresize").append('<img class="resizer" src="resizer.png"/>');
var r = true;
}
$(".resizer").mousedown(function(e){
if(r)
{
var Xoffset = this.parentNode.offsetLeft + this.parentNode.offsetWidth - e.clientX;
var Yoffset = this.parentNode.offsetTop + this.parentNode.offsetHeight - e.clientY;
r = false;
}
$(".resizer").mousemove(function(e){
this.parentNode.offsetWidth = e.clientX + Xoffset - this.parentNode.offsetLeft;
this.parentNode.offsetHeight = e.clientY + Yoffset - this.parentNode.offsetTop;
}
$(".resizer").mouseup(function(){
r = true;
}
}
它还需要与脚本位于同一文件夹中的图像,最好使用 10x10 或更小的图标作为调整 div 元素大小的图标,但不仅调整大小不起作用,图像甚至不显示,这让我相信用于检查 CSS 的变量是导致问题的原因,因为没有它,图像永远不会附加到 div 元素内。但由于我没有编写代码,而且我找不到其他代码来检查是否支持特定的 CSS 属性,所以我无法真正找到错误。如果您要尝试测试代码,您还需要在样式标记内添加这段代码。
.resize
{
z-index:2;
position:absolute;
right:5;
bottom:5;
}
任何修复代码的帮助将不胜感激!
I'm using CSS's resize property to resize a div element in FF, Safari, and Chrome, but since this property doesn't work in IE and Opera, I am forced to find a backup in case one of these two browsers is being used, so I'm using a combination of Jquery and javascript, to ask a function I found online, here: Browser resize support code
. Unfortunately, this is the code I fear isn't working in my test to see if it would work.
Here's my test code: My code
var unsupported = (function()
{
var div = document.createElement('div'),
vendors = 'Khtml Ms O Moz Webkit'.split(' '),
len = vendors.length;
return function(prop)
{
if( prop in div.style ) return true;
prop = prop.replace(/^[a-z]/, function(val)
{
return val.toUpperCase();
});
while(len--)
{
if ( vendors[len] + prop in div.style )
{
return false;
}
}
return true;
};
})();
if( unsupported('resize') )
{
$("#elementtoresize").append('<img class="resizer" src="resizer.png"/>');
var r = true;
}
$(".resizer").mousedown(function(e){
if(r)
{
var Xoffset = this.parentNode.offsetLeft + this.parentNode.offsetWidth - e.clientX;
var Yoffset = this.parentNode.offsetTop + this.parentNode.offsetHeight - e.clientY;
r = false;
}
$(".resizer").mousemove(function(e){
this.parentNode.offsetWidth = e.clientX + Xoffset - this.parentNode.offsetLeft;
this.parentNode.offsetHeight = e.clientY + Yoffset - this.parentNode.offsetTop;
}
$(".resizer").mouseup(function(){
r = true;
}
}
It additionally requires an image in the same folder as the script, preferably 10x10 or smaller to be used as the icon for resizing the div element, but not only does the resizing not work, the image doesn't even display, which is what leads me to believe that the variable for checking the CSS is what's causing the problem, because without it the image is never appended inside the div element. But since I didn't write the code and there were no other codes I could find for checking to see if the specific CSS property is supported, I have no way of really finding the error. If you're going to try testing the code, you'll also need this bit of code inside the style tag.
.resize
{
z-index:2;
position:absolute;
right:5;
bottom:5;
}
Any help fixing the code would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码在很多方面都是错误的,包括语法和方法。
首先,您应该使用像 Modernizr 这样的库来测试功能支持。无论如何,如果您要使用此代码,请尝试不要仅更改其中的一部分。函数
unsupported
失败,因为您没有更改它的所有返回点。以后如果确实需要使用新的命名,请将原来的函数包装起来。就像unsupported = function (x) { return !supported(x); };
而且,你原来使用的函数是不完整的。虽然它不影响您的具体情况,但我冒昧地改进了它。在这里找到它: http://jsfiddle.net/mihaibirsan/NCs5J/
其次,调整大小的代码是真的很乱。当您将函数作为参数传递时,不要忘记关闭函数调用的括号!另外,使用全局变量绝对是不好的——不惜一切代价避免!此外,既然您使用的是 jQuery,为什么不将这些功能包装在一个漂亮的小 jQuery 函数中呢?您可以在这里找到代码:http://jsfiddle.net/mihaibirsan/Rty3R/8/
因此,您的代码应该归结为之前粘贴的零碎内容(没有测试代码),再加上以下行:
我希望这会有所帮助!如果您满意,请采纳答案!我这样做是为了积分!
That code is wrong in many ways, including syntax and approach.
First of all, you should use a library like Modernizr for testing feature support. Anyway, if you're going with this code, try not changing just parts of it. The function
unsupported
fails because you didn't change all of it's return points. In the future, wrap the original function if you really need to use new naming. Likeunsupported = function (x) { return !supported(x); };
Moreover, the original function you used was incomplete. While it didn't affect your particular case, I took the liberty to improve it. Find it here: http://jsfiddle.net/mihaibirsan/NCs5J/
Second, the resizing code was really messy. When you pass functions as parameters, don't forget to close the parenthesis of the function call! Also, using global variables is absolutely bad—avoid at all costs! Besides, since you're using jQuery, why not wrap the functionality in a nice little jQuery function? You can find the code here: http://jsfiddle.net/mihaibirsan/Rty3R/8/
So your code should come down to the bits and pieces pasted before (without the testing code), plus the following line:
I hope this helps! If it pleases you, please accept the answer! I do this for the points!