【问题标题】:Slide in link on hover over悬停时滑入链接
【发布时间】:2013-09-18 15:00:25
【问题描述】:

我想创建以下操作:

当您将鼠标移到固定在屏幕左侧的导航栏中的一段文本上时,第二段文本(或图像,如果这样更容易的话)将从左侧滑出出现的页面。第二个文本或图像将位于第一个文本或图像之下(效果可能偏移)。在 mouseoff 时,上述效果会反转,您将只剩下第一段文字。

我希望这是有道理的,我一直在拖网过渡和悬停在网络上的效果,但我无法完全找到我的设想。

【问题讨论】:

标签: javascript hover transition


【解决方案1】:

以下是我为您拼凑的一些东西作为示例。它使用jQuery:

HTML

<div class="text">
    <a>Text One</a>
    <a>Text Two</a>
</div>

CSS

.text{
    width:200px;
    position:relative;
}

.text > a{

    position:relative;
    left:-100%;
    display:none;

}

.text > a:first-child{

  left:0;
  display:block;

}

JavaScript

$(document).ready(function(){ // here we're only running the code when the page is ready

    $('.text > a:first-child').hover(function(){ // here we're targeting the first .text element

      $(this).siblings().css('display', 'block'); // we're making it visible
      $(this).siblings().animate({'left':'0%'}); // we're animating it out of it's hidden state

    }, function(){ // this function is passed as the second argument to the 'hover' method

      $(this).siblings().animate({'left':'-100%'}, 500, function(){ // animate back to hidden state

        $(this).css('display', 'none'); // when the animation is complete, hide it completely

    });
  });
});

这是 jsFiddle 上的 working example

显然这个概念可以优化/开发......

【讨论】:

  • 不,这很完美,这正是我的想法。非常感谢你
  • 您好 Shenan,您的代码看起来像我所追求的,尽管我在尝试将 javascript 调用到 html 时遇到问题。我的 html 正文中有 ,但似乎没有什么区别(悬停是我所说的你的 javascript)
  • @JJmason 首先,请注意您需要 jQuery 库来运行它。 jQuery 是一个非常有用的库,用于遍历 DOM 和执行 CSS 更改。您可以访问 [jQuery](www.jquery.com) 网站并下载它,也可以使用 Google 托管的版本here - 只需在您的&lt;head&gt; 中包含&lt;script&gt; 标记。之后,检查hover.js 是否实际被加载。使用浏览器的开发人员工具来执行此操作(大多数浏览器都有它们)。您也可以使用开发控制台检查任何其他可能的错误。
  • @JJmason 我已经稍微调整了代码。我已将 cmets 放入 JavaScript 中,这样您就可以准确地看到每一行发生了什么。我还用 jQuery ready 方法将整个事情包装起来,这意味着脚本只有在所有 HTML 元素都准备好后才运行。这也意味着您可以将您的&lt;script src='hoverover.js&gt;&lt;/script&gt;&lt;body&gt; 中取出(最好的做法)并将其放在文档的&lt;head&gt; 中。
  • 感谢深南,我已将 jquery 添加到 html 文档中,但没有任何改变。然而,在检查代码时,我确实收到了这个错误:未捕获的 ReferenceError: $ is not defined
猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2017-09-19
  • 2013-02-09
  • 1970-01-01
  • 2013-04-30
相关资源
最近更新 更多