简体   繁体   English

在元素单击上切换CSS样式

[英]Switch CSS styles on element click

Sorry if this question has been asked a lot. 抱歉,这个问题问了很多。 I am currently working on a site that will have a custom gallery. 我目前正在一个拥有自定义图片库的网站上工作。 Of all things to get stuck on right now, its the gallery page indicator. 现在所有卡住的东西,其图库页面指示器。

On this page, the left portion will be made of a gallery of "galleries", displaying 6 galleries per "page". 在此页面的左侧,将由“画廊”画廊构成,每“页面”显示6个画廊。 http://www.ct-social.com/ctsdev/aff/news-and-events/ http://www.ct-social.com/ctsdev/aff/news-and-events/

Above the gallery are small blue dots that will serve as gallery indicators. 画廊上方是小蓝点,将用作画廊指示。 The code to create the gallery indicators is as follows: 创建图库指示器的代码如下:

    <?php for ($i=0; $i < $n2; $i++): ?>
    <div class="event-gallery-unselected"></div>
    <?php endfor; ?>

Upon loading I would like the left most dot to be given a different style that is attributed to class="event-gallery-selected". 加载后,我希望为最左边的圆点赋予不同的样式,该样式归因于class =“ event-gallery-selected”。 Upon clicking any other dot (except the current selection) the currently selected dot needs to revert back to "event-gallery-unselected" and the clicked dot takes on "event-gallery-selected" 单击任何其他点(当前选择除外)后,当前选择的点需要恢复为“未选择事件库”,并且单击的点将变为“已选择事件库”

I am kinda new to PHP, very new to JavaScript and JQuery. 我是PHP的新手,JavaScript和JQuery的新手。 If using either of those languages as an example could you please breakdown your explanation? 如果以这些语言中的任何一种为例,请您解释一下? Thank you very much for all of your help. 非常感谢您的所有帮助。

Updated code: CSS 更新的代码:CSS

.event-gallery.selected {
position: relative; 
top: -0.7em;  
background: white;
background-color: #1e93bb;
width: 20px;
height: 20px;
margin: 7px;
border-radius: 50%; 
}
.event-gallery {    
position: relative; 
top: -1.1em;  
background: white;
background-color: #63c5e7;
width: 15px;
height: 15px;
border-radius: 50%;
margin: 7px;
float: right;
cursor: pointer;
}

Updated Code JS 更新了代码JS

<script type="text/javascript">
jQuery(document).ready(function($){
    $(".event-gallery").click(function() {
        $(this).addClass('selected').siblings().removeClass('selected');
    });
});
</script>

Just got it working now. 现在就开始工作了。

I would suggest having a class which is present on all of your gallery div elements. 我建议您在所有Gallery div元素上都使用一个类。 This will allow the common styles to be maintained and also allow you to have only 1 click handler. 这将允许保留常用样式,并且还只有一个单击处理程序。 You can then have a separate selected class which you toggle as needed. 然后,您可以有一个单独的selected类,可以根据需要进行切换。 Try this: 尝试这个:

<?php for ($i = 0; $i < $n2; $i++): ?>
    <div class="event-gallery"></div>
<?php endfor; ?>
.event-gallery { 
    color: #000; /* default styling ... */
    padding: 5px;
}
.event-gallery.selected { 
    color: #FFF; /* selected item styling ... */
    background-color: #C00;
}
$(".event-gallery").click(function() {
    $(this).addClass('selected').siblings().removeClass('selected');
});

Example fiddle 小提琴的例子

use jQuery like this: 像这样使用jQuery:

$('divid').click(function(){
    $('divid').css('background-image', 'pic2.jpeg');
});

for example 例如

If you have a set of elements: 如果您具有一组元素:

<div class="wrapper">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

I typically handle it as such: 我通常这样处理:

var $items = $('#wrapper .item'); 
$('.item').click(function(){
    $items.removeClass('active'); // 'reset' the active links
    $(this).addClass('active'); // apply the active class to the clicked item
})

This can easily be done with a little bit of help from jQuery. 借助jQuery的一点帮助,即可轻松完成此操作。

$(function() {
    $(".even-gallery-unselected").on("click", function() {
        this.removeClass("event-gallery-unselected")
            .addClass("event-gallery-selected");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM