CSS3 translate out of screen

后端 未结 8 1691
南旧
南旧 2021-02-07 07:48

For a number of projects now I have had elements on the page which I want to translate out of the screen area (have them fly out of the document). In proper code this should be

8条回答
  •  青春惊慌失措
    2021-02-07 08:07

    One very simple, but not aesthetically pleasing solution is to define the class dynamically:

    var stylesheet = document.styleSheets[0];
    var ruleBlockHide;
    

    and

    //onresize
    if(ruleBlockHide) stylesheet.deleteRule(ruleBlockHide);
    ruleBlockHide = stylesheet.insertRule('.block.hide{ -webkit-transform:translateY(-'+window.innerHeight+'px); }',stylesheet.cssRules.length);
    

    see: http://jsfiddle.net/PDU7T/

    The reason a reference to the rule needs to be kept is that after each screen resize the rule has to be deleted and re-added.

    Although this solution gets the job done, there has to be some DOM/CSS combination which would allow this to be done without javascript (something along the lines of a 100%x100% element containing such a block, but I haven't been able to figure out any transform based way).

提交回复
热议问题