Language

HTML + CSS/JQuery

JQuery - 화면 이동에 따른 회전, 곡선, 이동

초코렛맛 2024. 7. 15. 07:37
<style>
    body{
        height: 600vh;
        position: relative;
    }
    .abc{
        width: 300px;
        height: 300px;
        background-image: url("https://picsum.photos/id/121/300/300");
        position: fixed;
        transition: all 0.2s;
        left: 0px;
    }
</style>

<body>
    <div class="abc"></div>

</body>
<script>
    $(function(){
        $(window).scroll(function(){
            let a = $(window).scrollTop();
            let maxa = $(document).height()-$(window).height();
            let halfa = maxa/2;
            let ra;
            if(a<=halfa){
                ra = parseInt(a/halfa *150);
                $(".abc").css("border-radius",ra+"px");
                $(".abc").css("transform",'rotate(0deg)');
                $(".abc").css("left","0px");
                
            }
            else{
                ra = parseInt(a/halfa * 360-360);
                ww = $(window).width()-$(".abc").width();
                lt = parseInt(a/halfa *ww - ww);
                $(".abc").css("border-radius",150+"px");
                $(".abc").css("transform",'rotate(' + ra + 'deg)');
                $(".abc").css("left",lt+"px");
            }            
            console.log(ra+"   "+a/halfa);
        });
    });
</script>