【问题标题】:jQuery fade in/out replaced text removes linkjQuery淡入/淡出替换文本删除链接
【发布时间】:2013-07-18 15:29:41
【问题描述】:

演示: jsFiddle

这在淡入/淡出部分正常工作,但是h1子作为href标签在hover上被删除-我想保留a 标签。

这也会导致 -webkit-text-fill-color: transparent; 出现问题-webkit-background-clip:文本;所以如果我要为 a 标签设置动画,它会导致一个跳跃的动画(chrome)。但是我发现如果我将父级设置为 h1 动画运行顺利

结构应该是这样的:

<div id="heroburrito">
    <div class="vert">
         <h1>
            <a class="homehover" href="#"></a>  <!--This parts gets removed on hover - it shouldn't-->
        </h1>
    </div>
</div>

js

$('#heroburrito .vert h1 a.homehover').attr('data-originalText', function () {
    return (this.textContent || this.innerText).trim();
}).hover(function () {
    $(this).fadeOut(660, function () {
        $(this).text('←retreat').fadeIn().addClass('home').idle(200);
    });
},

function () {
    $(this).fadeOut(660, function () {
        $(this).text($(this).attr('data-originalText')).fadeIn().removeClass('home');
    });
});

【问题讨论】:

  • 这是一种奇怪的css实现方式。设置class或者id然后把css放到css box里查看会更快更方便
  • @OliverCole 抱歉,这是直接取自我正在做的 WP 主题。我在哪里使用 PHP 控制 CSS - 检查此链接 sebastiangraz.com/projects/portfolio/?portfolio=konsthallen
  • @Oliver 的方式并不奇怪,它是方式之一(内联样式)。唯一的好处是它们具有最高优先级,无论如何都会应用这些样式。

标签: javascript jquery html css


【解决方案1】:

好吧,您正在使用$(this).text(...) - 这将替换this 引用的元素的全部内容 - 这是您的h1 元素。相反,您应该将代码附加到h1 内的a 元素:

$('#heroburrito .vert h1 a.homehover').hover(...)

这在你的问题中是正确的,但你的小提琴只包含

$('#heroburrito .vert h1').hover(...)

因此用纯文本替换您的整个链接。这是我的updated fiddle,它工作正常。

编辑:如果您需要在 h1 而不是链接本身上运行淡入/淡出,那么您需要将文本更改应用于链接 - 这是一个 updated fiddle:

$('#heroburrito .vert h1').hover(function () {
    $(this).fadeOut(200, function () {
        $('a.homehover', this).text('←retreat');
        $(this).fadeIn().addClass('home')
    });
},

function () {
    $(this).fadeOut(200, function () {
        $('a.homehover', this).text($(this).attr('data-originalText'));
        $(this).fadeIn().removeClass('home');
    });
});

【讨论】:

  • 是的,我知道这一点。抱歉,我可能应该在帖子中提到这一点 - 但是这会导致 -webkit-text-fill-color: transparent; 出现问题-webkit-background-clip:文本;正如您在小提琴中看到的那样,文本淡入/淡出在 chrome 中非常跳跃。但是,如果您为 h1 而不是锚标记设置动画,它会平滑淡出/淡入
  • 这正是我想要的。谢谢!
【解决方案2】:
$('#heroburrito .vert h1').attr('data-originalText', function () {
    return (this.textContent || this.innerText).trim();
}).hover(function () {
    $(':first-child:not',this).fadeOut(200, function () {
        $(this).text('←retreat').fadeIn().addClass('home');
    });
},
function () {
    $(':first-child:not',this).fadeOut(200, function () {
        $(this).text($(this).attr('data-originalText')).fadeIn().removeClass('home');
    });
});

这将使a 元素保持不变。

JSFIDDLE

【讨论】:

  • Uncaught TypeError: Cannot call method 'replace' of undefined jquery.js?ver=1.8.3:2 我得到那个错误
【解决方案3】:

我编辑了 JSFiddle 以解决您更改 h1 的 innerText 而不是 a 标签的 innerText 的问题。

http://jsfiddle.net/n4HCQ/14/

你应该这样做

$(this).find("a").text('←retreat');
$(this).fadeIn().addClass('home');

代替

$(this).text('←retreat').fadeIn().addClass('home').idle(200);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 2018-03-02
    • 1970-01-01
    • 2014-03-08
    • 2023-03-15
    • 2021-07-04
    • 1970-01-01
    相关资源
    最近更新 更多