2

I am a beginner and I am unable to implement the smooth scroll for contact. I want to be able to click on the contact and it should scroll down smoothly to the contacts form.

document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});
:root{
    --mainColor:#eaeaea;
    --secondaryColor: #fff;

    --borderColor: #c1c1c1;

    --mainText: black;
    --secondaryText: #4b5156;

    --themeDotBorder: #24292e;
    
    --previewBg:rgb(251, 249, 243, 0.8);

    --previewShadow:#f0ead6;

    --buttonColor:black;

    --b1: #2c2938 0%;
    --b2: #73c5cf 29%;
    --b3:#cc5d5d 67%;
    --b4: #78ce5e 100%;

}



html{
    padding: 0;
    margin: 0;
    scroll-behavior: smooth;
}

body{
    padding: 0;
    margin: 0;
}

body *{
    transition: 0.5s;
}

h1{
    color: var(--mainText);
    font-family: 'Russo One', sans-serif;
    font-weight: 500;
}
h2,h3,h4,h5,h6, strong{
    color: var(--mainText);
    font-family: 'Russo One', sans-serif;
    font-weight: 500;
}

p, li, span, label, input, textarea{
    color: var(--secondaryText);
    font-family: 'Roboto Mono', monospace;
}

a{
    text-decoration: none;
    color: #17a2b8;
}

ul{
    list-style: none;
}

h1 { font-size: 56px;}
h2 { font-size: 36px;}
h3 { font-size: 28px;}
h4 { font-size: 24px;}
h5 { font-size: 20px;}
h6 { font-size: 16px;}

.s1{
    background-color: var(--mainColor);
    border-bottom:1px solid var(--borderColor);
    overflow:auto;
}

.animate-charcter
{
   text-transform: uppercase;
  background-image: linear-gradient(
    -225deg,
    var(--b1),
    var(--b2),
    var(--b3),
    var(--b4)
  );
  background-size: auto auto;
  background-clip: border-box;
  background-size: 200% auto;
  color: #fff;
  background-clip: text;
  text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textclip 5s linear infinite;
  display: inline-block;
      font-size: 60px;
}

@keyframes textclip {
  to {
    background-position: 200% center;
  }
}



.s2{
    background-color: var(--secondaryColor);
    border-bottom:1px solid var(--borderColor);
    overflow:auto;
}
.main-container{
    width: 1200px;
    margin: 0 auto;
}

.greeting-wrapper{
    display: grid;
    text-align: center;
    align-content: center;
    min-height: 10em;
}

.intro-wrapper{
    background-color: var(--secondaryColor);
    border: 1px solid var(--borderColor);
    border-radius: 5px 5px 0 0;

    -webkit-box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);
    -moz-box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);
    box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);

    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 3em 30em;
    grid-template-areas: 
        'nav-wrapper nav-wrapper'
        'left-column right-column'
        ;
}

.nav-wrapper{
    border-radius: 5px 5px 0 0;
    grid-area: nav-wrapper;
    border-bottom: 1px solid var(--borderColor);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--mainColor);
}

#navigation a{
    color:var(--mainText);
}

#navigation{
    margin: 0;
    padding: 10px;
}

#navigation li{
    display: inline-block;
    margin-right: 5px;
    margin-left: 5px;
}

.dots-wrapper{
    display: flex;
    padding: 10px;
}

#dot-1{
    background-color: #ED6B60;
}
#dot-2{
    background-color: #F5BF4F;

}
#dot-3{
    background-color: #73BE65;

}

.browser-dot{
    background-color: black;
    height: 15px;
    width: 15px;
    border-radius: 50%;
    margin: 5px;

    -webkit-box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52); 
    box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52);
    
}

.left-column{
    grid-area: left-column;
    padding-top: 50px;
    padding-bottom: 50px;
}

#codingpic{
    display: block;
    margin:0 auto;
    max-width:55%;
    height:auto;
    object-fit: cover;
}

#theme-options-wrapper{
    display: flex;
    justify-content: center;
}

.theme-dot{
    height: 30px;
    width: 30px;
    background-color: black;
    border-radius: 50%;
    margin: 5px;
    border: 2px solid var(--themeDotBorder);

    -webkit-box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52); 
    box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52);

    cursor: pointer;
}

.theme-dot:hover{
    border-width: 5px;
}

#light-mode{
    background-color: #fff;
}

#dark-mode{
    background-color: rgb(0, 0, 0);
}

#settings-note{
    font-size: 12px;
    font-style: italic;
    text-align: center;
}

.right-column{
    grid-area: right-column;
    display: grid;
    align-content: center;

    padding-top: 50px;
    padding-bottom: 50px;
}

#preview-shadow{
    background-color: var(--previewShadow);
    max-width: 300px;
    height: 180px;
    padding-left: 30px;
    padding-top: 30px;
}

#preview{
    width: 300px;
    border:1.5px solid #17a2b8;
    background-color: var(--previewBg);
    padding: 15px;
    position: relative;
}

.corner{
    width:7px;
    height: 7px;
    border-radius: 50%;
    border:1.5px solid #17a2b8;
    background-color: #fff;
    position: absolute;
}

#corner-tl{
    top:-5px;
    left: -5px
}

#corner-tr{
    top:-5px;
    right: -5px
}


#corner-br{
    bottom:-5px;
    right: -5px
}


#corner-bl{
    bottom:-5px;
    left: -5px
}

.about-wrapper{
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    padding-bottom: 50px;
    padding-top: 50px;
    gap:100px;
}


#skills{
    display: flex;
    justify-content: space-evenly;
    background-color: var(--previewShadow);
}

#contact-form{
    display: block;
    max-width: 600px;
    margin: 0 auto;
    border: 1px solid var(--borderColor);
    padding: 15px;
    border-radius: 5px;
    background-color: var(--mainColor);
    margin-bottom: 50px;

}

#contact-form label{
    line-height: 2.7em;
}

#contact-form textarea{
    min-height: 100px;
    font-size: 14px;
}

.input-field{
    width: 100%;
    padding-top: 10px;
    padding-bottom:10px; 
    background-color: var(--secondaryColor);
    border-radius: 5px;
    border:1px solid var(--borderColor);
    font-size: 14px;
}

#submit-btn{
    margin-top: 10px;
    width: 100%;
    padding-top: 10px;
    padding-bottom:10px; 
    color: #fff;
    background-color: var(--buttonColor);
    border:none;
}

@media screen and (max-width: 1200px){
    .main-container{
        width: 95%;
    }
}

@media screen and (max-width: 800px){
    .intro-wrapper{
        grid-template-columns: 1fr;
        grid-template-areas: 
            'nav-wrapper'
            'left-column'
            'right-column'
        ;
    }

    .right-column{
        justify-content: center;
    }

}
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1" />

    <link href="https://fonts.googleapis.com/css2?family=Russo+One&display=swap" rel="stylesheet">

    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@500&family=Russo+One&display=swap" rel="stylesheet">

    <link rel="stylesheet" type="text/css" href="default.css">
    <link id="theme-style" rel="stylesheet" type="text/css" href="">
    <link id="theme-style" rel="stylesheet" type="text/css" href="">
    
</head>
<body>
    <section class = "s1">
        <div class = "main-container">
            <div class = "greeting-wrapper">
                <h1 class="animate-charcter">Hello, I'm Test</h1>
            </div>
            <div class = "intro-wrapper">
                <div class = "nav-wrapper">
                    <div class = "dots-wrapper">
                        <div id="dot-1" class="browser-dot"></div>
                        <div id="dot-2" class="browser-dot"></div>
                        <div id="dot-3" class="browser-dot"></div> 
                    </div>

                    <ul id = "navigation">
                        <a class="scroll"href="index.html#contact">Contact</a>
                    </ul>

                </div>
    
                <div class = "left-column">
                    <img id="codingpic" src="images/code.png">
                    <h5 style="text-align: center;line-height: 0;">Personalize Theme</h5>
                    <div id="theme-options-wrapper">
                        <div data-mode="light" id="light-mode" class="theme-dot"></div>
                        <div data-mode="black"  id="black-mode" class="theme-dot"></div>
                    </div>
                    <p id="settings-note"> These setting will be saved <br> for your next visit</p></div>
    
                <div class = "right-column">

                    <div id="preview-shadow">
                        <div id="preview">
                            <div id="corner-tl" class="corner"></div>
                            <div id="corner-tr" class="corner"></div>
                            <h3>What I Do</h3>
                            <p>I was a lead developer in a past life, now I enjoy teaching courses.</p>
                            <div id="corner-br" class="corner"></div>
                            <div id="corner-bl" class="corner"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class = "s2">
        <div class = "main-container">
            <div class="about-wrapper">
                <div class="about-me"></div>
                <h4>More about myself</h4>
                <p>I build apps and love to work with new ideas to revolutionize the world</p>
                <p>blah blah blah blah blah blah doooo blah</p>
            </div>
            <hr>
            <h4>Top Expertixe</h4>
            <p>Front End developer with focus on HTML, CSS, and Javascript: <a target="_blank" href="resume.pdf">Download Resume</a></p>
            <div id="skills">
                <ul>
                    <li>Python</li>
                    <li>Django</li>
                    <li>JavaScript</li>
                    <li>React</li>
                    <li>Postgres</li>
                </ul>

                <ul>
                    <li>Google Maps API</li>
                    <li>JS Charts</li>
                    <li>AWS (RDS/S3)</li>
                    <li>Heroku</li>
                    <li>HTML/CSS</li>
                </ul>

            </div>
        </div>
    </div>
</div>
</div>


    </section>

    <section class="s1">
        <div class="main-container">
            <h3 style="text-align: center;">Some of my past projects</h3>
            <div class="post-wrapper">
            <div>
    </section>

    <section class = "s2">
        <div class="main-container">
            <a href=""></a>
            <h3 style="text-align: center;">Get In Touch</h3>

            <form id="contact-form">
                <a name="contact"></a>

                <label>Name</label>
                <input class="input-field" type="text" name="name">

                <label>Subject</label>
                <input class="input-field" type="text" name="subject">

                <label>Email</label>
                <input class="input-field" type="text" name="email">

                <label>Message</label>
                <textarea class="input-field" name="message"></textarea>

                <input id="submit-btn" type="submit" value="Send">
            </form>
        </div>
    </section>

    <script type="text/javascript" src="script.js"></script>
    <script src="scroll.js"></script>


</body>
</html>

I am confused on where exactly this is going wrong, I have tried many different ways, I'm sure ive been close but since I have recently started, I am clearly missing something and running by it.

DevitySire
  • 21
  • 2
  • the code `document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); });` is aim to click the a link and scroll to the ancher? i checked your code ,but don't get any a link who's href is start at `#` – screw spike Dec 16 '21 at 03:10
  • so are you sure the code `document.querySelectorAll('a[href^="#"]')` can get all selector ? – screw spike Dec 16 '21 at 03:12

1 Answers1

0

It seems to be a simple case of wrong labeling.

('a[href^="#"]') finds only elements when the href starts with #. Also, the target to scroll to has the id contact-form, so I changed the link to

<a class="scroll" href="#contact-form">Contact</a>

and it seems to be working fine.

document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        document.querySelector(this.getAttribute('href')).scrollIntoView({
            behavior: 'smooth'
        });
    });
});
:root{
    --mainColor:#eaeaea;
    --secondaryColor: #fff;

    --borderColor: #c1c1c1;

    --mainText: black;
    --secondaryText: #4b5156;

    --themeDotBorder: #24292e;
    
    --previewBg:rgb(251, 249, 243, 0.8);

    --previewShadow:#f0ead6;

    --buttonColor:black;

    --b1: #2c2938 0%;
    --b2: #73c5cf 29%;
    --b3:#cc5d5d 67%;
    --b4: #78ce5e 100%;

}



html{
    padding: 0;
    margin: 0;
    scroll-behavior: smooth;
}

body{
    padding: 0;
    margin: 0;
}

body *{
    transition: 0.5s;
}

h1{
    color: var(--mainText);
    font-family: 'Russo One', sans-serif;
    font-weight: 500;
}
h2,h3,h4,h5,h6, strong{
    color: var(--mainText);
    font-family: 'Russo One', sans-serif;
    font-weight: 500;
}

p, li, span, label, input, textarea{
    color: var(--secondaryText);
    font-family: 'Roboto Mono', monospace;
}

a{
    text-decoration: none;
    color: #17a2b8;
}

ul{
    list-style: none;
}

h1 { font-size: 56px;}
h2 { font-size: 36px;}
h3 { font-size: 28px;}
h4 { font-size: 24px;}
h5 { font-size: 20px;}
h6 { font-size: 16px;}

.s1{
    background-color: var(--mainColor);
    border-bottom:1px solid var(--borderColor);
    overflow:auto;
}

.animate-charcter
{
   text-transform: uppercase;
  background-image: linear-gradient(
    -225deg,
    var(--b1),
    var(--b2),
    var(--b3),
    var(--b4)
  );
  background-size: auto auto;
  background-clip: border-box;
  background-size: 200% auto;
  color: #fff;
  background-clip: text;
  text-fill-color: transparent;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: textclip 5s linear infinite;
  display: inline-block;
      font-size: 60px;
}

@keyframes textclip {
  to {
    background-position: 200% center;
  }
}



.s2{
    background-color: var(--secondaryColor);
    border-bottom:1px solid var(--borderColor);
    overflow:auto;
}
.main-container{
    width: 1200px;
    margin: 0 auto;
}

.greeting-wrapper{
    display: grid;
    text-align: center;
    align-content: center;
    min-height: 10em;
}

.intro-wrapper{
    background-color: var(--secondaryColor);
    border: 1px solid var(--borderColor);
    border-radius: 5px 5px 0 0;

    -webkit-box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);
    -moz-box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);
    box-shadow: 12px 14px 7px 2px rgba(0,0,0,0.75);

    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 3em 30em;
    grid-template-areas: 
        'nav-wrapper nav-wrapper'
        'left-column right-column'
        ;
}

.nav-wrapper{
    border-radius: 5px 5px 0 0;
    grid-area: nav-wrapper;
    border-bottom: 1px solid var(--borderColor);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: var(--mainColor);
}

#navigation a{
    color:var(--mainText);
}

#navigation{
    margin: 0;
    padding: 10px;
}

#navigation li{
    display: inline-block;
    margin-right: 5px;
    margin-left: 5px;
}

.dots-wrapper{
    display: flex;
    padding: 10px;
}

#dot-1{
    background-color: #ED6B60;
}
#dot-2{
    background-color: #F5BF4F;

}
#dot-3{
    background-color: #73BE65;

}

.browser-dot{
    background-color: black;
    height: 15px;
    width: 15px;
    border-radius: 50%;
    margin: 5px;

    -webkit-box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52); 
    box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52);
    
}

.left-column{
    grid-area: left-column;
    padding-top: 50px;
    padding-bottom: 50px;
}

#codingpic{
    display: block;
    margin:0 auto;
    max-width:55%;
    height:auto;
    object-fit: cover;
}

#theme-options-wrapper{
    display: flex;
    justify-content: center;
}

.theme-dot{
    height: 30px;
    width: 30px;
    background-color: black;
    border-radius: 50%;
    margin: 5px;
    border: 2px solid var(--themeDotBorder);

    -webkit-box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52); 
    box-shadow: 1px 3px 9px -1px rgba(0,0,0,0.52);

    cursor: pointer;
}

.theme-dot:hover{
    border-width: 5px;
}

#light-mode{
    background-color: #fff;
}

#dark-mode{
    background-color: rgb(0, 0, 0);
}

#settings-note{
    font-size: 12px;
    font-style: italic;
    text-align: center;
}

.right-column{
    grid-area: right-column;
    display: grid;
    align-content: center;

    padding-top: 50px;
    padding-bottom: 50px;
}

#preview-shadow{
    background-color: var(--previewShadow);
    max-width: 300px;
    height: 180px;
    padding-left: 30px;
    padding-top: 30px;
}

#preview{
    width: 300px;
    border:1.5px solid #17a2b8;
    background-color: var(--previewBg);
    padding: 15px;
    position: relative;
}

.corner{
    width:7px;
    height: 7px;
    border-radius: 50%;
    border:1.5px solid #17a2b8;
    background-color: #fff;
    position: absolute;
}

#corner-tl{
    top:-5px;
    left: -5px
}

#corner-tr{
    top:-5px;
    right: -5px
}


#corner-br{
    bottom:-5px;
    right: -5px
}


#corner-bl{
    bottom:-5px;
    left: -5px
}

.about-wrapper{
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    padding-bottom: 50px;
    padding-top: 50px;
    gap:100px;
}


#skills{
    display: flex;
    justify-content: space-evenly;
    background-color: var(--previewShadow);
}

#contact-form{
    display: block;
    max-width: 600px;
    margin: 0 auto;
    border: 1px solid var(--borderColor);
    padding: 15px;
    border-radius: 5px;
    background-color: var(--mainColor);
    margin-bottom: 50px;

}

#contact-form label{
    line-height: 2.7em;
}

#contact-form textarea{
    min-height: 100px;
    font-size: 14px;
}

.input-field{
    width: 100%;
    padding-top: 10px;
    padding-bottom:10px; 
    background-color: var(--secondaryColor);
    border-radius: 5px;
    border:1px solid var(--borderColor);
    font-size: 14px;
}

#submit-btn{
    margin-top: 10px;
    width: 100%;
    padding-top: 10px;
    padding-bottom:10px; 
    color: #fff;
    background-color: var(--buttonColor);
    border:none;
}

@media screen and (max-width: 1200px){
    .main-container{
        width: 95%;
    }
}

@media screen and (max-width: 800px){
    .intro-wrapper{
        grid-template-columns: 1fr;
        grid-template-areas: 
            'nav-wrapper'
            'left-column'
            'right-column'
        ;
    }

    .right-column{
        justify-content: center;
    }

}
<!DOCTYPE html>
<html>
<head>
    <title>Test</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1" />

    <link href="https://fonts.googleapis.com/css2?family=Russo+One&display=swap" rel="stylesheet">

    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@500&family=Russo+One&display=swap" rel="stylesheet">

    <link rel="stylesheet" type="text/css" href="default.css">
    <link id="theme-style" rel="stylesheet" type="text/css" href="">
    <link id="theme-style" rel="stylesheet" type="text/css" href="">
    
</head>
<body>
    <section class = "s1">
        <div class = "main-container">
            <div class = "greeting-wrapper">
                <h1 class="animate-charcter">Hello, I'm Test</h1>
            </div>
            <div class = "intro-wrapper">
                <div class = "nav-wrapper">
                    <div class = "dots-wrapper">
                        <div id="dot-1" class="browser-dot"></div>
                        <div id="dot-2" class="browser-dot"></div>
                        <div id="dot-3" class="browser-dot"></div> 
                    </div>

                    <ul id = "navigation">
                        <a class="scroll"href="#contact-form">Contact</a>
                    </ul>

                </div>
    
                <div class = "left-column">
                    <img id="codingpic" src="images/code.png">
                    <h5 style="text-align: center;line-height: 0;">Personalize Theme</h5>
                    <div id="theme-options-wrapper">
                        <div data-mode="light" id="light-mode" class="theme-dot"></div>
                        <div data-mode="black"  id="black-mode" class="theme-dot"></div>
                    </div>
                    <p id="settings-note"> These setting will be saved <br> for your next visit</p></div>
    
                <div class = "right-column">

                    <div id="preview-shadow">
                        <div id="preview">
                            <div id="corner-tl" class="corner"></div>
                            <div id="corner-tr" class="corner"></div>
                            <h3>What I Do</h3>
                            <p>I was a lead developer in a past life, now I enjoy teaching courses.</p>
                            <div id="corner-br" class="corner"></div>
                            <div id="corner-bl" class="corner"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <section class = "s2">
        <div class = "main-container">
            <div class="about-wrapper">
                <div class="about-me"></div>
                <h4>More about myself</h4>
                <p>I build apps and love to work with new ideas to revolutionize the world</p>
                <p>blah blah blah blah blah blah doooo blah</p>
            </div>
            <hr>
            <h4>Top Expertixe</h4>
            <p>Front End developer with focus on HTML, CSS, and Javascript: <a target="_blank" href="resume.pdf">Download Resume</a></p>
            <div id="skills">
                <ul>
                    <li>Python</li>
                    <li>Django</li>
                    <li>JavaScript</li>
                    <li>React</li>
                    <li>Postgres</li>
                </ul>

                <ul>
                    <li>Google Maps API</li>
                    <li>JS Charts</li>
                    <li>AWS (RDS/S3)</li>
                    <li>Heroku</li>
                    <li>HTML/CSS</li>
                </ul>

            </div>
        </div>
    </div>
</div>
</div>


    </section>

    <section class="s1">
        <div class="main-container">
            <h3 style="text-align: center;">Some of my past projects</h3>
            <div class="post-wrapper">
            <div>
    </section>

    <section class = "s2">
        <div class="main-container">
            <a href=""></a>
            <h3 style="text-align: center;">Get In Touch</h3>

            <form id="contact-form">
                <a name="contact"></a>

                <label>Name</label>
                <input class="input-field" type="text" name="name">

                <label>Subject</label>
                <input class="input-field" type="text" name="subject">

                <label>Email</label>
                <input class="input-field" type="text" name="email">

                <label>Message</label>
                <textarea class="input-field" name="message"></textarea>

                <input id="submit-btn" type="submit" value="Send">
            </form>
        </div>
    </section>

    <script type="text/javascript" src="script.js"></script>
    <script src="scroll.js"></script>


</body>
</html>
Swimmer F
  • 843
  • 5
  • 15
  • I tried using that, and it didn't work for me, its scrolls down to contacts but it doesn't scroll down smoothly, without a cut. – DevitySire Dec 16 '21 at 03:18
  • Are you using Safari? Safari has no support for smooth behaviour of `scrollIntoView`. How to get around that is answered [here](https://stackoverflow.com/questions/51229742/javascript-window-scroll-behavior-smooth-not-working-in-safari) – Swimmer F Dec 16 '21 at 03:26
  • Ahh yes that was it, thanks it was solved by using chrome. – DevitySire Dec 16 '21 at 22:23