Execute your code only upon window resize.
Instead, organize it within a function and invoke it when the page loads, then link it to the window resize
event listener.
function checkWidth() {
if ($(this).width() < 760) {
$('.circular-text').hide();
} else {
$('.circular-text').show();
}
}
checkWidth();
$(window).resize(checkWidth);
const text = document.querySelector(".circular-text .contact-text")
const rotate = new CircleType(text).radius(65)
window.addEventListener("scroll", function() {
text.style.transform = `rotate(${window.scrollY * 0.15}deg)`
});
.circular-text {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: fixed;
bottom: 5px;
right: 70px;
z-index: 999999;
}
.contact-text {
font-family: "Alliance No 2";
font-weight: 700;
font-size: 13px;
text-transform: uppercase;
color: #fb4d98;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07646e75646b62737e7762473529342937">[email protected]</a>/dist/circletype.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="contact-container">
<div class="circular-text">
<p class="contact-text">contact us • contact us • contact us •</p>
</div>
</div>
To rectify the fading issue, include an if
statement to verify if the width exceeds 760px:
function checkWidth() {
if ($(this).width() < 760) {
$('.circular-text').hide();
} else {
$('.circular-text').show();
}
}
checkWidth();
$(window).resize(checkWidth);
const text = document.querySelector(".circular-text .contact-text")
const rotate = new CircleType(text).radius(65)
window.addEventListener("scroll", function() {
text.style.transform = `rotate(${window.scrollY * 0.15}deg)`
});
$(document).on('scroll', function() {
var distanceFromBottom = Math.floor($(document).height() - $(document).scrollTop() - $(window).height());
if (distanceFromBottom < 350) {
$('#contact-fixed').fadeOut();
if ($(this).width() > 760) {
$('.circular-text').fadeOut();
}
} else {
$('#contact-fixed').fadeIn();
if ($(this).width() > 760) {
$('.circular-text').fadeIn();
}
}
});
body {
height: 1000px;
}
.circular-text {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: fixed;
bottom: 5px;
right: 69px;
z-index: 999999;
}
.contact-text {
font-family: "Alliance No 2";
font-weight: 700;
font-size: 13px;
text-transform: uppercase;
color: #fb4d98;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccafa5beafa0a9b8b5bca98cfee2ffe2fc">[email protected]</a>/dist/circletype.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="contact-container">
<div class="circular-text">
<p class="contact-text">contact us • contact us • contact us •</p>
</div>
</div>
To settle the issue of the library improperly re-rendering the text on resize, we can intelligently re-render it in the resize
event listener.
Final Result:
const text = document.querySelector(".circular-text .contact-text")
function checkWidth() {
const rotate = new CircleType(text).radius(65)
if ($(this).width() < 760) {
$('.circular-text').hide();
} else {
$('.circular-text').show();
}
}
checkWidth();
$(window).resize(checkWidth);
window.addEventListener("scroll", function() {
text.style.transform = `rotate(${window.scrollY * 0.15}deg)`
});
$(document).on('scroll', function() {
var distanceFromBottom = Math.floor($(document).height() - $(document).scrollTop() - $(window).height());
if (distanceFromBottom < 350) {
$('#contact-fixed').fadeOut();
if ($(this).width() > 760) {
$('.circular-text').fadeOut();
}
} else {
$('#contact-fixed').fadeIn();
if ($(this).width() > 760) {
$('.circular-text').fadeIn();
}
}
});
body {
height: 1000px;
}
.circular-text {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: fixed;
bottom: 5px;
right: 69px;
z-index: 999999;
}
.contact-text {
font-family: "Alliance No 2";
font-weight: 700;
font-size: 13px;
text-transform: uppercase;
color: #fb4d98;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c1a2a8b3a2ada4b5b8b1a481f3eff2eff1">[email protected]</a>/dist/circletype.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="contact-container">
<div class="circular-text">
<p class="contact-text">contact us • contact us • contact us •</p>
</div>
</div>