简体   繁体   English

如何在 body 标签上设置 CSS 以使用 JavaScript 旋转页面

[英]How to set CSS on body tag to rotate a page using JavaScript

I found the following code while reading a related question at this page: Rotate a webpage clockwise or anticlockwise我在本页阅读相关问题时发现了以下代码: 顺时针或逆时针旋转网页

html {
     -webkit-transform: rotate(180deg);
     -moz-transform: rotate(180deg);
}

-- ——

My question is how do I set this CSS via JavaScript?我的问题是如何通过 JavaScript 设置这个 CSS?

When a visitor clicks on an image, I want to use JavaScript to modify the HTML tag's style using the settings above.当访问者单击图像时,我想使用 JavaScript 使用上述设置修改 HTML 标记的样式。

Thanks in advance.提前致谢。

The easiest way to do this is by putting this in a class, and assigning that class dynamically using JavaScript:最简单的方法是将它放在一个类中,并使用 JavaScript 动态分配该类:

CSS: CSS:

html.rotateme {
     -webkit-transform: rotate(180deg);
     -moz-transform: rotate(180deg);
}

JavaScript: JavaScript:

document.documentElement.setAttribute('class', 'rotateme'); //Note that this will override the current class

Since you're using a very recent CSS feature, you probably don't need to support older browsers.由于您使用的是最新的 CSS 功能,因此您可能不需要支持较旧的浏览器。 Therefore, you can also use Element.classList to add a class.因此,您也可以使用Element.classList添加一个类。

To prevent this from throwing an exception in older browsers, you can check for the existance of it first:为了防止在旧浏览器中抛出异常,您可以先检查它是否存在:

JavaScript: JavaScript:

document.documentElement.classList && document.documentElement.classList.add('rotateme'); //This won't  override the current class

See also: Change an element's class with JavaScript另请参阅:使用 JavaScript 更改元素的类

You use the style attribute on the HTML node.您在 HTML 节点上使用style 属性

Something like this will do it:像这样的事情会做到:

var htmlNode = document.documentElement;
htmlNode.style.webkitTransform = 'rotate(180deg)';
htmlNode.style.mozTransform = 'rotate(180deg)';

Then, to undo the rotate, set the values to an empty string:然后,要撤消旋转,请将值设置为空字符串:

var htmlNode = document.documentElement;
htmlNode.style.webkitTransform = '';
htmlNode.style.mozTransform = '';

Simple简单的

$('#wrap').on('click',function(){
$('html').css('-webkit-transform','rotate(180deg)');
$('html').css('-moz-transform','rotate(180deg)');})
**JQUERY**

$("#imgID").on("click", function(){
    $('html').css({"-webkit-transform": "rotate(180deg)", "moz-transform": "rotate(180deg)"});
});

only js只有js

use user2428118's answer, it's a solid solution.使用 user2428118 的答案,这是一个可靠的解决方案。

define a class for this:-为此定义一个类:-

.htmlrotate {
     -webkit-transform: rotate(180deg);
     -moz-transform: rotate(180deg);
}

onclick of the button, run this:-单击按钮,运行:-

document.documentElement.classList.add('htmlrotate');
  $('html').bind('click', function () {
                $('html').css({
                    '-webkit-transform': 'rotate(90deg)',
                    '  -moz-transform': 'rotate(90deg)'
                })
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM