切换类/ Id

时间:2017-09-16 19:26:01

标签: javascript jquery html css

所以对于我的网站,我想要一个类似系统的完成,你可以告诉你完成了一个视频或漫画。我意识到我应该有一个class AnimatedWidget : public QWidget { Q_OBJECT Q_PROPERTY(qreal alpha READ alpha WRITE setAlpha) public: AnimatedWidget(QWidget* parent = nullptr) :QWidget{ parent }, opacityAnimation{ new QPropertyAnimation{this, "alpha",this} } { setWindowFlags(windowFlags() | Qt::WindowFlags(Qt::FramelessWindowHint) | Qt::Tool); auto pal = palette(); pal.setColor(QPalette::Background, Qt::cyan); setAutoFillBackground(true); setPalette(pal); setFixedSize(200, 200); } qreal alpha() const { return windowOpacity(); } void setAlpha(qreal level) { setWindowOpacity(level); update(); } protected: void closeEvent(QCloseEvent* e) override { if (opacityAnimation->currentValue().toReal() == 1.0) { // Ignore event + start animation e->ignore(); startHide(); QObject::connect(opacityAnimation, SIGNAL(finished()), this, SLOT(onAnimationCallBack()), Qt::UniqueConnection); } else { e->accept(); if (!isHidden()) hide(); QWidget::close(); // necessary actions } } public Q_SLOTS: void show() { startShow(); QWidget::show(); // necessary actions } private Q_SLOTS: void onAnimationCallBack() { if (opacityAnimation->currentValue().toReal() == 0.0) { // we're finished so let's really close the widget QCloseEvent ev; QApplication::sendEvent(this, &ev); qApp->sendEvent(this, &ev); } } void startHide() { opacityAnimation->setStartValue(1.0); opacityAnimation->setEndValue(0.0); opacityAnimation->setDuration(1500); opacityAnimation->start(); } void startShow() { opacityAnimation->setStartValue(0.0); opacityAnimation->setEndValue(1.0); opacityAnimation->setDuration(1500); opacityAnimation->start(); } private: QPropertyAnimation* opacityAnimation = nullptr; }; class Base : public QWidget { public: Base(QWidget* parent = nullptr) :QWidget{ parent }, widget{ new AnimatedWidget{} } { } private: AnimatedWidget* widget; protected: void mouseReleaseEvent(QMouseEvent* e) override { if (widget->isHidden()) widget->show(); else widget->close(); QWidget::mouseReleaseEvent(e); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); Base base; base.show(); return app.exec(); } 开始变红,但你可以点击div变绿,但你也可以再次点击以切换回红色。我做了一些研究并获得了一些答案,例如divtoggleClassaddClass,但Javascript因某些原因无效。

基本示例:

removeClass
.undone /*Default Class*/
{width: 100px; height: 100px; background-color: red;}

.done /*Replacement Class*/
{width: 100px; height: 100px; background-color: green;}

1 个答案:

答案 0 :(得分:4)

处理click事件并在处理程序内切换.done

<强>的jQuery

$('#myDiv').on('click', function() {
   $(this).toggleClass('done');
});
.undone
{width: 100px; height: 100px; background-color: red;}

.done
{width: 100px; height: 100px; background-color: green;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv" class="undone"></div>

纯Javascript

const div = document.getElementById("myDiv");

div.addEventListener('click', function() {
   div.classList.toggle('done');
});
.undone
{width: 100px; height: 100px; background-color: red;}

.done
{width: 100px; height: 100px; background-color: green;}
<div id="myDiv" class="undone"></div>