jQuery旋转图像并相应地调整父元素

编程入门 行业动态 更新时间:2024-10-08 13:28:40
本文介绍了jQuery旋转图像并相应地调整父元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用css旋转来旋转图像.它工作正常,但未同时调整其他元素. 它与其他元素重叠.我想要的是如果我旋转图像,则它不应与其他元素重叠,而应相应地调整其高度和宽度. 请在下面查看我的代码

I am using css rotate to rotate an image. It is working fine but it is not adjusting other elements as well. It is overlapping other elements. What I want if i rotate an image then it shouldn't overlap the other elements but adjust its height and width accordingly. Please see my code below

HTML

<div id="wrapper"> <h1>Heading 1</h1> <img id="testimg" src="thecutestblogontheblock/wp-content/uploads/2012/10/Owl-Walk-free-cute-halloween-fall-blog-BANNER.png" alt="" /> <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> </div> <button>Rotate</button>

JQuery

var degrees = 0; $("button").click(function(){ degrees += 90; var img = $("#testimg"); img.css({ "-webkit-transform": "rotate(" + degrees + "deg)", "-moz-transform": "rotate(" + degrees + "deg)", "transform": "rotate(" + degrees + "deg)" /* For modern browsers(CSS3) */ }); });

请在JS小提琴中检查我的代码" jsfiddle/FcQZm/ 非常感谢您的帮助

Please check my code in JS fiddle "jsfiddle/FcQZm/" I will really appreciate your help

推荐答案

它不会自动发生,但是您可以使用getBoundingClientRect()告诉您转换后的整体高度和宽度,并使用其调整大小您的包装器(或其他元素)以适应/适应更改:

It won't happen automatically, but you can use getBoundingClientRect() to tell you what the overall height and width is after the transformation, and use that to resize your wrapper (or other elements) to fit/accommodate the change:

var angle = 0; $(function () { $("button").click(function () { angle = (angle + 30) % 360; var bounds = $("img#testimg").css({ "transform-origin": "50% 50%", "transform": "rotate("+angle+"deg)", })[0].getBoundingClientRect(); $("#wrapper").css({ width: bounds.width, height: bounds.height }); }); });

演示: jsfiddle/jtbowden/pnp1kyjh/1/

(适用于最新的Firefox,Chrome,IE).

(works in latest Firefox, Chrome, IE).

更多推荐

jQuery旋转图像并相应地调整父元素

本文发布于:2023-11-11 08:32:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   元素   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!