【问题标题】:targeting multiple classes at once一次定位多个类
【发布时间】:2013-06-05 19:38:20
【问题描述】:

我的页面上有一个基于 CSS 的悬停/点击效果,效果很好。当项目 (.print) 悬停时,全彩图像 (.print_photo) 会出现在右侧。单击该项目时,图像会变为灰色并出现一个文本框 (.print_text)。

点击功能仅在您按住点击时有效,我希望它在点击后保持可见,直到点击另一个项目。这可能吗?

(我没有足够的声誉来发布图像,一旦我发布它就会发布)图像大小是宽度:620px;高度:490px;

CSS

#bgtextbox{
    width:320px;
    height:391px;
    background-color:#BCBEC0;
    margin:130px 0 0 0px;
    position:absolute;
    text-align:center;
    font-family:Arial, Helvetica, sans-serif;
    z-index:1;
    }

/* hover/click START */
.print{
        width:340px;
        height:40px;
        background-color:#E6E7E8;
        margin:6px 0 0 0px;
        position:relative;
        text-align:center;
        font-family:Arial, Helvetica, sans-serif;
        font-weight:bold;
        line-height:40px;
        border:1px solid #E6E7E8;
        z-index:12;
        }

.print_photo{
        width:620px;
        height:490px;
        margin:-48px 0 0 370px;
        text-align:center;
        background-repeat:no-repeat;
        position:absolute;  
        z-index:2;      
        }

.print_photo img{
            opacity:0;
            max-height:100%;
            max-width:100%;
            } 

.print_text{
            width:430px;
            height:150px;
            margin:292px 0 0 397px;
            position:absolute;
            border-radius: 20px / 20px;
            opacity:.75;
            color:transparent;
            z-index:13;
            }


.print:hover{
            border:1px solid #F15A24;
            cursor:pointer;             
            }

.print:hover ~ .print_photo img{
                            opacity:1;
                            -webkit-transition: opacity 1s ease-in-out;
                      -moz-transition: opacity 1s ease-in-out;
                      -o-transition: opacity 1s ease-in-out;
                      transition: opacity 1s ease-in-out;                                       
                            }

.print:active ~ .print_photo img{   
                        filter: grayscale(100%);
                        -webkit-filter: grayscale(100%);
                        -moz-filter: grayscale(100%);
                        -ms-filter: grayscale(100%);
                        -o-filter: grayscale(100%);
                        opacity:.5;
                        -webkit-transition: opacity 1s ease-in-out;
                      -moz-transition: opacity 1s ease-in-out;
                      -o-transition: opacity 1s ease-in-out;
                      transition: opacity 1s ease-in-out;
                        }

.print:active ~ .print_text{                            
                    background-color:#000;
                    color:#FFF;
                    }
    /* END */

HTML

<div id="bgtextbox">
  <div class="print">PRINT</div>
  <div class="print_photo"><img src="images/print.png"</div></div>
  <div class="print_text">PRINT TEXT GOES HERE</div>
</div>

【问题讨论】:

  • 你必须有某种javascript来设置点击后的样式,CSS不会在点击时设置样式。
  • 我对JS不是很了解,而且我找到的所有模板或示例都只针对一个元素。 @PatrickEvans
  • 您可以将事件处理程序添加到多个元素,但 事件委托 可能是您应该使用的。

标签: html css hover


【解决方案1】:

为此,您将需要 Javascript。实际上有一种技术可以使用单选按钮和纯 css 来做到这一点,但由于它实际上是一种 hack,而且很脏,我将直接转向 jquery 解决方案。

您必须在现有的 css 中添加一些选择器:

.print.active  ~ .print_text, .print:active ~ .print_text {
.print.active ~ .print_photo img, .print:active ~ .print_photo img {

您会注意到,样式现在不仅会在鼠标按下时触发(:active),而且还会在它包含 .active 类时触发

使用几行 jQuery,您可以在点击时切换该类:

// when print is clicked
$('.print').click(function() {
    // remove the old active
    $('.print.active').removeClass('active');
    // add the active class to the trigger
    $(this).addClass('active');
});

可以在此处找到一个工作示例: http://jsfiddle.net/WRwVf/

编辑:
要在您的页面中包含此代码,您必须首先加载 jQuery 库。添加这样的东西作为你身体的最后一个节点:

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

然后你可以在下面放置你的脚本。请注意,将它放在“就绪”事件中也是明智的。像这样的:

<script type="text/javascript">
// when the DOM is ready
$(document).ready(function() {
   /* - The above code goes here - */
});
</script>

你也可以把脚本放在一个单独的.js文件中,和jquery库一样加载它,但是由于它只是几行代码,这会被一些人认为是矫枉过正,因为额外的http 请求会减慢您的页面速度。

【讨论】:

  • jQuery 从未被提及。即使您使用 JS 解决方案,您也不需要引入整个库来执行此操作。
  • 一个额外的 32kb 文件只是为了写 3 行代码?你称之为“工作”?
  • 如果你使用谷歌 CDN 之类的东西,90% 的时间库已经在浏览器缓存中,所以这不是真正的问题 imo。但这不值得讨论......
  • 答案是不要为 jQuery 的使用辩护。它学习javascript。
  • 我应该把 jQuery 放在哪里? @PeterVR
【解决方案2】:

您将需要为此使用 JS。让一些 JS 在您的一个 .print 元素的 onClick 上运行,它会向其中添加一个“选定”类,首先从所有其他元素中删除该类。

【讨论】:

    【解决方案3】:

    你必须使用 JS 来设置一个类,然后在需要时删除它。

    HTML

    <div id="bgtextbox">
         <div id="print" class="print">PRINT</div>
         <div class="print_photo"><img src="images/print.png"</div></div>
         <div class="print_text">PRINT TEXT GOES HERE</div>
    </div>
    

    CSS

    .printactive ~ .print_photo img{   
        filter: grayscale(100%);
        -webkit-filter: grayscale(100%);
        -moz-filter: grayscale(100%);
        -ms-filter: grayscale(100%);
        -o-filter: grayscale(100%);
        opacity:.5;
        -webkit-transition: opacity 1s ease-in-out;
        -moz-transition: opacity 1s ease-in-out;
        -o-transition: opacity 1s ease-in-out;
        transition: opacity 1s ease-in-out;
    }
    
    .printactive ~ .print_text{                            
        background-color:#000;
        color:#FFF;
    }
    

    JS

    document.getElementById("print").addEventListener("click",activatePrintDiv);
    
    function activatePrintDiv(){
        var pclass = this.getAttribute("class");
        this.setAttribute("class",pclass+" printactive");
    }
    

    【讨论】:

    • 这个功能,但它只是解决方案的一部分:cssdeck.com/labs/65kdnk1c。当点击其他元素时,这里没有禁用点击的代码。
    • 嗯,我希望他能自己填写那部分。
    【解决方案4】:

    正如@DuncanLock 推荐的那样,最简单的解决方案是为此使用 JS。更有创意(但基于 CSS)的方法是创建一个 .print 的兄弟,这是一个复选框。

    <div id="bgtextbox">
        <div class="print">PRINT</div>
        <input type="checkbox" class="print_checkbox" />
        <div class="print_photo"><img src="images/print.png"</div></div>
        <div class="print_text">PRINT TEXT GOES HERE</div>
    </div>
    

    将其 CSS 设置为:

    .print_checkbox {
        position:absolute;
        top:0;
        left:0;
        right:0;
        bottom:0;
        opacity:0.01;
    }
    

    所以它填满了 div 的整个区域,并且看起来是透明的。我应该向不经意的观察者指出,您还需要设置父级 (#bgtextbox) 的 position,但他已经在他的 CSS 中这样做了。

    然后让 CSS 使用 :checked psuedo-class 根据是否选中(单击)显示 img。只需改变这个:

    .print:active ~ .print_photo img
    

    到这里:

    .print_checked:checked + .print_photo img
    

    您仍然需要 IE8- 的 JS 解决方案,但无论如何您都已经需要它,使用 ~ CSS 选择器,因此在浏览器兼容性方面没有任何区别。

    只是思考的食物。这并不完全是 WYSIWYG 编码方法,但如果您是那种尝试尽可能利用 CSS 而不是 JS 的开发人员(如我),这是一个很酷的小技巧。

    【讨论】:

    • 正如他所说,他只希望图片保持可见,直到单击另一个“按钮”,您需要使用单选按钮执行此操作:jsfiddle.net/WRwVf 但我仍然认为这是一个 hack ,我不会在现实生活中使用它。仅为样式添加元素在语义上是不正确的...我同意您应该始终尝试使用无 js 的解决方案,但在这种情况下,我会选择 js(甚至 jQuery ;-))
    • 我已经明确了这是什么......我很高兴你同意自己使用 jQuery 哈哈
    猜你喜欢
    • 2021-06-08
    • 2018-09-30
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多