热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何将jQuery.hover()与溢出结合使用-HowtousejQuery.hover()incombinationwithOverflow

Well,iamstuckedandcantfindtheanswermyself.Hopefullysomeonecangivemeahint.好吧,我被困了,自

Well, i am stucked and can't find the answer myself. Hopefully someone can give me a hint.

好吧,我被困了,自己找不到答案。希望有人可以给我一个提示。

I try to fullfill the following requirements:

我尝试满足以下要求:

  • There should be a Newsblock within a HTML Page with a fixed width and height.
  • HTML页面中应该有一个具有固定宽度和高度的新闻块。

  • In this Newsblock only the title of the news are visible.
  • 在这个新闻块中,只有新闻标题可见。

  • Those news are "collapsed" by default and should "expand" if the Mouse is over it.
  • 默认情况下,这些新闻会“崩溃”,如果鼠标位于其上,则应该“展开”。

  • Due the fact that the 'Newsblock' is limited by its height, there should be a Scrollbar visible. But only if the currently expanded news makes it necessary, so the user can Scroll down.
  • 由于“新闻块”受其高度的限制,因此应该可以看到滚动条。但只有当前扩展的新闻使其成为必要时,用户才能向下滚动。

  • Newstitle and Newstext should never leave the Newsblock.
  • Newstitle和Newstext永远不应该离开Newsblock。

so far so good, i was able to fullfill all those demands except the one with the Scrollbar. If i try to reach the Scrollbar out of the currently expanded news it collapses again and the Scrollbar disappears. I understand that my .hover is configured that it always SlideUp if i leave the newsentry and the Scrollbar isn't a part of the newsentry div. But i have no idea what to change to still have an overall Scrollbar for the Newsblock, but won't disappear if i try to 'reach' it.

到目前为止一切顺利,我能够满足所有这些要求,除了带有滚动条的那个。如果我尝试从当前扩展的新闻中获取滚动条,它会再次折叠并且滚动条消失。我知道我的.hover配置为如果我离开newsentry并且Scrollbar不是newsentry div的一部分它总是SlideUp。但我不知道要改变什么仍然有新闻块的整体滚动条,但如果我试图“达到”它将不会消失。

P.s.: A Scrollbar only per Newsentry looks weird. Thats why i want 'bind' the scrollbar to the parent container :S

P.s。:只有Newsentry的Scrollbar看起来很怪异。这就是为什么我想把滚动条'绑定'到父容器:S

HTML

    
// some auto generated div's i have to life with, so the news entries are not 'direct' children of the newsblock.
...
...
... another 9 'newsentry' divs.

JS

    $(".newsentry").hover(
        function() {
            $(this).children(".newstext").stop(true,true).slideDown();
        },
        function() {
            $(this).children(".newstext").stop(true,true).slideUp();
        }
    );

CSS

    .newsblock {
        height: 200px;
        overflow-y: auto;
    }

4 个解决方案

#1


1  

Instead of closing a .newsentry when the cursor goes out of it, a solution can be to close it only when it enters another .newsentry or when it leaves #newsblock. The scrollbar being part of #newsblock, the entry isn't closed anymore when you go on it.

当光标离开时,不是关闭.newsentry,而是只有当它进入另一个.newsentry或离开#newsblock时才能关闭它。滚动条是#newssblock的一部分,当你继续它时,该条目不再关闭。

EDIT: Following our discussion about the scroll issue, I added a step callback to the closing animation to make sure that the top of the .newsentry getting opened remains visible when the other entries are getting closed.

编辑:在我们讨论了滚动问题之后,我添加了一个关闭动画的步骤回调,以确保在其他条目关闭时,.newsentry的顶部仍然可见。

Here is a working example:

这是一个工作示例:

var $newsblock = $("#newsblock");

function closeAllNews(slideUpArgs){
    return $(".newstext").stop(true).slideUp(slideUpArgs);
}

function openNews(news, slideDownArgs){
    $(news).find(".newstext").stop(true).slideDown(slideDownArgs);
}

function ensureNewsTopVisible(news){
  // Check if the top of the newsentry is visible...
  var top = $(news).position().top;
  if(top <0){
    // ...and if not, scroll newsblock accordingly.
    $newsblock.scrollTop($newsblock.scrollTop() + top);
  }
}

$(".newsentry").each(function(){
    var $this = $(this);
    // When the mouse enter a news entry...
    $this.on("mouseenter", function(){
        // ...close all opened entries (normally there is at most one)...
        closeAllNews({
          // (while making sure that the top of this entry remains visible
          // at each step)
          step: ensureNewsTopVisible.bind(null, $this)
        });
        // ...open this newsentry.
        openNews($this);
    });
});

// When the mouse get out of the newsblock, close all news.
$newsblock.on("mouseleave", closeAllNews);
.newstitle {
    font-size: 2em;
}
.newstext {
    display: none;
}
#newsblock {
    max-height: 150px;
    overflow: scroll;
}


News 1
News 2
News 3

#2


0  

Try this:

$(".newsentry, .newsblock").hover( // <-- changed
        function() {
            $(this).children(".newstext").stop(true,true).slideDown();
        },
        function() {
            $(this).children(".newstext").stop(true,true).slideUp();
        }
    );

This makes sure the block stays open when you hover either over the header or the block itself.

当您将鼠标悬停在标题或块本身上时,这可确保块保持打开状态。

Is that what you mean?

你是这个意思吗?

#3


0  

There would be a joke , if i am wrong .. what i thing just change your css as

如果我错了,会有一个笑话。我只想改变你的css

/* not .newsblock **/
      #newsblock {
                height: 200px;
                overflow-y: scroll;/* not auto*/
            }

#4


0  

It will be a lot better if you use click operation instead of hover to slide down news text block because the user can accidentally hover over any of the news entry in order to reach for the scroll bar. I think you need a accordion like functionality. You can use the below code if you are fine with click instead of hover.

如果您使用点击操作而不是悬停以向下滑动新闻文本块,那将会更好,因为用户可能会意外地悬停在任何新闻条目上以便到达滚动条。我想你需要一个类似手风琴的功能。如果你没有点击而不是悬停,你可以使用下面的代码。

$(".newsentry").click(
        function() {
            $(".newstext").stop(true,true).slideUp();
            $(this).children(".newstext").stop(true,true).slideDown();
        }
    );

Or use the below one to go with hover.

或者使用下面的一个来悬停。

$(".newsentry").hover(
    function() {
        $(".newstext").stop(true,true).slideUp();
        $(this).children(".newstext").stop(true,true).slideDown();
    },
    function(){}
);

This will not close the news text block until you accidentally hover over another news entry.

在您意外地将鼠标悬停在另一个新闻条目上之前,这不会关闭新闻文本块。


推荐阅读
author-avatar
书友59082326
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有