QML 日月交替效果


在这里插入图片描述


前言

过年闲来无事,学学前端,写写qml

一、效果

在这里插入图片描述

二、代码

主体为Sky.qml,直接调用即可

// Sky.qml
import QtQuick
import QtQuick.Controls
import Qt5Compat.GraphicalEffects

Item {
    id: control
    width: 600
    height: 400
    property bool isNight: false
    readonly property real displayY: 20
    readonly property color nightColor: "#040720"
    readonly property color daytimeColor: "#FFEEE2"

    Rectangle {
        id: content
        anchors.fill: parent
        color: isNight ? nightColor : daytimeColor

        Sun {
            visible: !isNight
            x: (parent.width - width) / 2
            y: visible ? displayY : parent.height - height
            Behavior on y {
                NumberAnimation {
                    duration: 500
                }
            }
        }

        Moon {
            visible: isNight
            x: (parent.width - width) / 2
            y: visible ? displayY : 0
            nightColor: control.nightColor
            Behavior on y {
                NumberAnimation {
                    duration: 500
                }
            }
        }
    }

    FastBlur {
        source: content
        width: source.width
        height: source.height / 3
        anchors.bottom: parent.bottom
        transparentBorder: true
        radius: 50
    }

    Button {
        text: "切换"
        onClicked: isNight = !isNight
    }
}

两个组件:Sun.qml和Moon.qml

// Sun.qml
import QtQuick
import QtQuick.Controls

Rectangle{
    width: 200
    height: 200
    radius: width / 2
    color: "orange"
    clip: true
}
// Moon.qml
import QtQuick
import QtQuick.Controls

Rectangle{
    property color nightColor: "#040720"
    
    width: 200
    height: 200
    radius: width / 2
    color: "cyan"
    clip: true
    
    Rectangle{
        width: 200
        height: 200
        color: nightColor
        radius: width / 2
        anchors.left: parent.left
        anchors.leftMargin: - width / 3
        anchors.top: parent.top
        anchors.topMargin: -height / 3
    }
}

三、参考视频

HTML5+CSS3+JS实现日月交替效果

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值