悬停在一个 div 上并将 CSS 应用于另一个 div

3
我有一个ID为#homeSectionB的div容器,里面有几篇文章,它们都有.testimonial类。
我的目标是当你悬停在其中一篇testimonial上时,其他所有testimonial的不透明度都变为0.60。我已经让它可以工作了,在这里可以看到实际效果:http://www.loaistudio.com/client-preview/Loai%20Design%20Studio
但是,我遇到了一个问题:当你悬停在容器上时,所有的testimonial都获得了这个opacity类。我该如何修复它?并且只有当你实际悬停在其中一篇testimonial上时才会发生,而不是容器。
以下是示例代码:http://jsfiddle.net/svZ3C/
HTML:

                        <article class="testimonial">
                        <img alt="Neal Kilburne​​" src="assets/elements/neal-kilburne.jpg"/>
                            <section>
                                <h3>Neal Kilburne​​</h3>
                                <p>CEO, iTEQ Global www.iteqglobal.com</p>
                                    <br>
                                <p>“Loai is a great asset to our company and provides us with great and quick responses, such a talented designer which we have the honour of working with.” 2011 - 2012</p>
                            </section>  
                        </article>

                        <article class="testimonial">
                            <img alt="Vanessa Elharrar M.D​​​" src="assets/elements/vanessa-elharrar.jpg"/>
                            <section>
                                <h3>Vanessa Elharrar M.D​</h3>
                                <p>www.dreamcometruecoaching.com</p>
                                    <br>
                                <p>"“It was wonderful to work with Loai on my website. He’s a really talented and gifted web designer, really creative and really has an eye for style and design. I loved the process of working with him, he’s a really nice guy and I do recommend him. I think you’ll love the outcome. I was very impressed and would use him again in a heartbeat.”" November 25, 2012</p>
                            </section>
                        </article>

                        <article class="testimonial">
                            <img alt="Glen Eric Huysamer" src="assets/elements/glen-eric-huysamer.jpg"/>
                            <section>
                                <h3>Glen Eric Huysamer​</h3>
                                <p>Specialist Service Provider.</p>
                                    <br>
                                <p>“I would like to take this opportunity to warn people who might consider using Loai Design Studio. You will have to buckle up &amp; strap yourself in as this young designer &amp; associates take you through the process of creating your design needs. I was pleasantly surprised from start to finish, &amp; can say that even though Loai took control of the creative process the end result felt like it was mine. You can not go wrong with this young lad, go ahead surprise yourself”. December 30, 2011</p>
                            </section>
                        </article>

                    </div><div id="grid2"><!-----Gird 2----->   

                        <article class="testimonial">
                            <img alt="Geeta Martinez" src="assets/elements/geeta-martinez.jpg"/>
                            <section>
                                <h3 >Geeta Martinez</h3>
                                <p>Lawyer &amp; Business Consultant</p>
                                    <br>
                                <p>"Leo did a great job! He designed &amp; put together several websites for me in less than a week. He was incredibly patient &amp; flexible throughout the whole process, &amp; took a lot of the stress out of the whole situation for me. He is a really nice guy to work with - I really appreciated his approach! I would definitely recommend working with him". July 14, 2013</p>
                            </section>  
                        </article>  

                        <article class="testimonial">
                            <img alt="Phil Turtle" src="assets/elements/phil-turtle.jpg"/>
                            <section>
                                <h3>Phil Turtle</h3>
                                <p>www.turtleconsulting.com</p>
                                    <br>
                                <p>“When we needed a new logo for our business operation (Data Center Industry PR). We needed somebody to help to try to encapsulate what a Data Center is, into a very simple logo. As many people don not even know what a Data Center is that was not an easy challenge. But Loai Design stepped up and within two days we had several ideas from which to choose, we worked on the chosen one of those and very quickly indeed we had a full working logo and our ecommerce site up and running. Thank You Loai Design.” July 11, 2013</p>
                            </section>
                        </article>      

                        <article class="testimonial">
                            <section>
                                <h3>Richard Jackson</h3>
                                <p>Photographer www.rjpstudios.co.uk​</p>
                                    <br>
                                <p>“Loai designed my website last year though I could have done it myself loai added a proffesional touch to the design which is so important in creating the best first impeson.” 2013</p>
                            </section>
                        </article>                  

                    </div><div id="grid3"><!-----Gird 3----->   

                        <article class="testimonial">
                            <img alt="Lulu Oliver" src="assets/elements/lulu-oliver.jpg"/>
                            <section>
                                <h3>Lulu Oliver</h3>
                                <p>www.lusialpacas.co.uk</p>
                                    <br>
                                <p>"He is the most fantastic young man, he is talented, clever and extremely helpful. He Put my whole package together, he designed the website well. and now I have a website that I can mange my self and it just have everything I wanted to do. So I can't recommend him highly enough, he really is a really good guy, and if you want some help or you want him to do your website, that is the place to go!”  Mar 26, 2013</p>
                            </section>
                        </article>

                        <article class="testimonial">
                            <img alt="Alina Kouneva Tremblay" src="assets/elements/alina-kouneva-tremblay.jpg"/>
                            <section>
                                <h3>Alina Kouneva Tremblay</h3>
                                <p>www.alinagardens.com​</p>
                                    <br>
                                <p>"I had the honor and the privilege to work with Loai on my website. I have been looking for a web designer, who can simply just do what I wanted and put it in a website so that my customers had access to my work. He was wonderful to work with, as much he's a professional, he's also just a great guy and very easy to work with. I remember laughing lot with him and it's always a nice thing to do, becouse websites can be a bit stressfull. I love my website, my friends love my website, my business associate love my website, and I simply want everybody to love their websites as much as I do. So I highly recommend Loai for your next website!”  April 5, 2013</p>
                            </section>
                        </article>

                    </div>
                </div>
            </div>

CSS:

/*HOME PAGE -> Section E*/
.testimonial {
    background-color: #F7F7F7;
    border: 1px solid #149B78;

    margin-bottom: 20px;
    text-align: center;

    -webkit-transition: opacity 0.4s ease; 
    -moz-transition: opacity 0.4s ease; 
    -ms-transition: opacity 0.4s ease; 
    -o-transition: opacity 0.4s ease; 
    transition: opacity 0.4s ease;  

    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -ms-backface-visibility: hidden;
}

     .testimonial section {
        padding: 20px;
    }

         .testimonial section h3 {
                color: #1AC99C;
            }

        .testimonial section p {
            font-size: 0.90em;
        }

            .testimonial section p:first-of-type {
                color: #149B78;
            }

.testimonial img {
    border-bottom: 1px dashed #435D84;
    padding: 1px;
}


/*The hover-over animation*/
#homeSectionB:hover .testimonial {
    opacity: 0.60;
    -moz-opacity: 0.60;
    -khtml-opacity: 0.60;
    filter:alpha(opacity=60);   
}

    #homeSectionB:hover .testimonial:hover {
        opacity: 1;
        -webkit-box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
        -moz-box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
        box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
    }

1
请使用fiddle创建一个包含所有代码的示例,以便快速获得答案。 - Chandranshu
1
抱歉,这里有一个链接:http://jsfiddle.net/svZ3C/ - user2162270
提醒一下,此处与查看投资组合相关的按钮导致 404 错误。 - Doge
是的,我知道伙计,这个网站仍在开发中 - 我正在重新设计我的当前网站:www.loaidesign.co.uk - user2162270
2个回答

2

你的问题样式如下:

#homeSectionB:hover .testimonial {
    opacity: 0.60;
    -moz-opacity: 0.60;
    -khtml-opacity: 0.60;
    filter: alpha(opacity=60);
}

您已经明确指定,当有人在div#homeSectionB上悬停时,所有的推荐应该获得0.60的不透明度。现在,我理解您这样做是因为您希望当有人悬停在其中一个推荐上时,其他推荐会淡出。如果没有任何间距宽度,这将起作用。
现在有多种方法可以解决此问题。请告诉我您是否接受JS方法或仅使用CSS方法。
更新1:
在这里:http://jsfiddle.net/svZ3C/5/。 这里涉及的原则是您需要使用填充代替边距。以下是更新后的CSS规则:
article {
    padding-bottom: 20px;
    -webkit-transition: opacity 0.4s ease;
    -moz-transition: opacity 0.4s ease;
    -ms-transition: opacity 0.4s ease;
    -o-transition: opacity 0.4s ease;
    transition: opacity 0.4s ease;
}
#grid1 article {
    padding-right: 15px;
}
#grid2 article {
    padding: 0 5px 20px 5px;
}
#grid3 article {
    padding-left: 15px;
}

/*Don't modify articles. Modify testimonials within them.*/
#homeSectionB:hover .testimonial {
    opacity: 0.10;
    -moz-opacity: 0.10;
    -khtml-opacity: 0.10;
    filter:alpha(opacity=10);
}
#homeSectionB:hover .testimonial:hover {
    opacity: 1;
    -webkit-box-shadow: 0px 0px 20px 0px rgba(14, 109, 85, 0.3);
    -moz-box-shadow: 0px 0px 20px 0px rgba(14, 109, 85, 0.3);
    box-shadow: 0px 0px 20px 0px rgba(14, 109, 85, 0.3);
}

HTML稍作修改,变成了这样:

<article>
    <div class="testimonial" <img alt="Neal Kilburne​​" src="assets/elements/neal-kilburne.jpg" />
        <section>
            <h3>Neal Kilburne​​</h3>
            <p>CEO, iTEQ Global www.iteqglobal.com</p>
            <br>
            <p>“Loai is a great asset to our company and provides us with great and quick responses, such a talented designer which we have the honour of working with.” 2011 - 2012</p>
        </section>
    </div>
</article>

如果我们能坚持使用CSS就太好了,因为我之前用JS做过它,而且不想再用它了。 - user2162270

2
我们已经使设置工作正常(查看http://firststop.herokuapp.com上的导航栏),我知道您想要解决的问题是只有在悬停在其中一个元素上时才会产生透明度,而不是容器。
虽然我没有直接的解决方案,但我有一种方法可以很好地工作,让您的用户不会注意到任何事情。
CSS子选择器和同级选择器
我们花了很长时间研究CSS子选择器和同级选择器的机制,试图让它们工作以实现您想要的结果。问题在于,您无法在悬停时选择同级元素;您只能选择子元素:
.container:hover .children{
    /* Works */
}

.child:hover .other_children {
    /* Not Works */
}

可能有错,但这就是你所面临的问题——你必须从“容器向下”进行样式设计。
使用填充(Padding)
因此,我们的解决方法是用 padding 替换 margin ,这意味着如果您用鼠标进入容器,则必须将其悬停在其中一个元素上。
我们将元素放在一个小型容器中。这是 display:inline-block ,并且具有 padding: Xpx 我更新了你的jsFiddle以展示这些修复措施:
article {
    padding-bottom: 20px;

    -webkit-transition: opacity 0.4s ease; 
    -moz-transition: opacity 0.4s ease; 
    -ms-transition: opacity 0.4s ease; 
    -o-transition: opacity 0.4s ease; 
    transition: opacity 0.4s ease;  
}
.testimonial {
    background-color: #F7F7F7;
    border: 1px solid #149B78;
    text-align: center;

    -webkit-backface-visibility: hidden;
    -moz-backface-visibility: hidden;
    -ms-backface-visibility: hidden;
}
/*The hover-over animation*/
#homeSectionB:hover article {
    opacity: 0.10;
    -moz-opacity: 0.10;
    -khtml-opacity: 0.10;
    filter:alpha(opacity=10);   
}

    #homeSectionB:hover article:hover {
        opacity: 1;
        -webkit-box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
        -moz-box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
        box-shadow: 0px 0px 20px 0px rgba(14,109,85,0.3);
    }

 <article >
      <div class="testimonial">
         <img alt="Neal Kilburne​​" src="assets/elements/neal-kilburne.jpg"/>
         <section>
             <h3>Neal Kilburne​​</h3>
             <p>CEO, iTEQ Global www.iteqglobal.com</p>
             <br>
             <p>“Loai is a great asset to our company and provides us with great and quick responses, such a talented designer which we have the honour of working with.” 2011 - 2012</p>
          </section>  
      </div>
  </article>

感谢夸奖!它还没有完成(我们还有其他一些要修复的东西)。在这种情况下,我认为样式无关紧要。你正在遇到不透明度的问题,这仍然是CSS3。如果你检查我的JSFiddle,我为你实现了一个解决方案,看起来对我有效。 - Richard Peck
你能再给我们展示一下实时链接吗?我会看看它是否可以修复! - Richard Peck
1
你的网站仍然使用旧的代码格式。@Chandranshu和我都建议使用填充来解决你的问题。如何实现修复将决定其性能表现 - 如果你按照推荐的修复方法更新布局,即在<article>选择器上添加填充并将testimonial div移动到文章内部,你将能够更好地解决布局问题。 - Richard Peck
奇怪,由于某些原因它没有起作用,我做错了什么?请立即刷新页面。 - user2162270
1
不,我认为你不需要JS!我找到了问题。 - Richard Peck
显示剩余17条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接