【问题标题】:Flip text with javascript使用 javascript 翻转文本
【发布时间】:2014-09-24 02:30:31
【问题描述】:

我有一个沿 Y 轴旋转文本的按钮,使其具有镜像外观。由于某种原因,这不再起作用,因为按钮已放置在子级(弹出窗口)上,而要镜像的文本位于父级上。

是否有一个javascript函数可以用来在单击按钮时旋转父级上的文本/再次单击时将其旋转回来。 (最好是拨动开关)

这是我最初只有一个父页面时的样子:

HTML 链接:

       <li><a class="button small icon-text-height flipx" href="#" onclick="return false;"></a></li>

带有文本的 div 的 CSS:

    article .teleprompter
{
    padding: 300px 50px 1000px 100px;
    font-size: 30px !important;
    line-height: 86px;
    z-index: 1;
    background-color: #141414;
   -webkit-transform: translate3d(0,0,0);
    -moz-transform: translate3d(0,0,0);
    -ms-transform: translate3d(0,0,0);
    -o-transform: translate3d(0,0,0);
    transform: translate3d(0,0,0);
}

flipx 部分的 CSS:

article .teleprompter.flipx
{
    -webkit-transform: rotateY(180deg);
    -moz-transform: rotateY(180deg);
    -o-transform: rotateY(180deg);
    -ms-transform: rotateY(180deg);
    z-index: 1;
    pointer-events: none;
    padding: 300px 50px 1000px 100px !important;
}

我认为应该可以工作的 JS:

    <script>
        function flipTXT(color)
    {
    if (parent_window && !parent_window.closed) {
    parent_window.document.getElementById("teleprompter").style['-webkit-transform'] = rotateY(180deg);
    }
    }
</script>

【问题讨论】:

  • 您使用的 javascript 是什么?另外,按钮是否会出现在弹出窗口中而不是与文本相同的页面中?
  • @AlexMorales 原版没有 javascript 。我用一些我认为可以与我现在拥有的父子系统一起使用的 javascript 编辑了我的帖子
  • 您可以做的是相应地添加和删除类属性,而不是更改样式属性。所以如果你想让它翻转,添加 .flipx 类,当你想让它正常显示时,将其移除。
  • @AlexMorales 我不会真正使用 javascript,所以我不知道从哪里开始

标签: javascript jquery html css


【解决方案1】:

我认为下面 Bin 的代码中看到的两种解决方案之一可能对您有用:

http://jsbin.com/buqexusamuda/1/

HTML

<p>Card: Flip</p>
<div class="card" href="#">Hello</div>
<p>Card 2: Mirror</p>
<div class="card card2" href="#">Hello</div>

CSS

.card, .card2 {
  position: relative;
  animation: all 2.5s;
  perspective: 1000;  
  transition: 0.6s;
  transform-style: preserve-3d;
  width: 90px;
  height: 32px;
  text-align: center;
  line-height: 32px;
  z-index: 1;
  background-color: #ccc;
  color: #666;
}
.card2 { transform-origin: right center; }
.card.flip { transform: rotateY(180deg); }

脚本

jQuery(".card").click(function(){
  $(this).toggleClass("flip");
});

【讨论】:

  • 它没有做任何事情吗? Jquery 未定义
  • 我将如何制作它以便我单击一个按钮并翻转文本而不是单击文本来翻转它?
  • 上述代码前需要包含jQuery:&lt;script src="//code.jquery.com/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
【解决方案2】:

最简单的解决方案是使用 jQuery 添加/删除类。如果你可以包含 jQuery,那么你可以按照以下方式做一些事情:

<script>
$(document).ready(function(){
    //Since the text is on the parent, you need to access it.
    var parentWindow = window.opener;

    //This gets the parent's DOM so you can grab the text from the parent window.
    var parentDom = parentWindow.document;

    //This grabs the text you want to transform.
    var targetText = parentDom.getElementsByClassName("teleprompter");

    //This toggles the class
    $(".button").on('click', function(){
        $(targetText).toggleClass("flipx");
    });

});
</script>

我使用了 jQuery 和常规 javascript 的组合,因此您不必滚动自己的代码来添加/删除和检查类。

下面是在你的页面中包含 jQuery 的代码,以防你手边没有它:

此版本适用于较旧的非 HTML 5 兼容浏览器和现代浏览器。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

这个只适用于更现代的浏览器:

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

【讨论】:

  • @Jamie - 您需要粘贴代码以将 jQuery 包含到您的 html 文件中。您可以在页面底部的
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 2011-03-26
  • 2021-10-09
  • 1970-01-01
  • 2012-09-12
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多