Live Chat Icon For mobile
Live Chat Icon

How do I add UI features, such as smooth scrolling that are already available in jQuery UI components?

To use jQuery UI components in a Blazor application, follow these steps:

Add jQuery reference to host the page

Add jQuery and its UI component script and style references in the ~/_Host.cshtml file.

Initialize the jQuery component

Initialize the jQuery components in the OnAfterRender lifecycle method in your Blazor application by using the JavaScript Interop’s InvokeVoidAsync method.

Refer to the following code samples.

[_Host.cshtml]

….
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="~/script.js"></script>
….

[Index.razor]

@page "/"
@inject IJSRuntime jsRuntime

    <div id="top">
        <a href="#section1">Go Section 1</a>
        <a href="#section2">Go Section 2</a>
        <a href="#section3">Go Section 3</a>
    </div><br>
    <div id="section1">
        In the web project, pagination is a very important part where huge numbers of records are listed from the
        database. In that case, Ajax pagination is a preferable way because it will help to improve the User
        Interface
        of your website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class.
        Pagination class helps to generate the pagination links and get the records from the database using Ajax.In
        the
        web project, pagination is a very important part where huge numbers of records are listed from the database.
        In
        that case, Ajax pagination is a preferable way because it will help to improve the User Interface of your
        website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class. Pagination
        class
        helps to generate the pagination links and get the records from the database using Ajax.
    </div>
    <br /><br />
    <div id="section2">
        <a href="#top">BackToTop</a><br />
        In the web project, pagination is a very important part where huge numbers of records are listed from the
        database. In that case, Ajax pagination is a preferable way because it will help to improve the User
        Interface
        of your website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class.
        Pagination class helps to generate the pagination links and get the records from the database using Ajax.In
        the
        web project, pagination is a very important part where huge numbers of records are listed from the database.
        In
        that case, Ajax pagination is a preferable way because it will help to improve the User Interface of your
        website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class. Pagination
        class
        helps to generate the pagination links and get the records from the database using Ajax.
    </div>
    <br /><br />
    <div id="section3">
        <a href="#top">BackToTop</a><br />
        In the web project, pagination is a very important part where huge numbers of records are listed from the database. In that case, Ajax pagination is a preferable way because it will help to improve the User Interface of your website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class.

Pagination class helps to generate the pagination links and get the records from the database using Ajax. In the
        web project, pagination is a very important part where huge numbers of records are listed from the database.
        In
        that case, Ajax pagination is a preferable way because it will help to improve the User Interface of your website. You'll easily be able to implement the Ajax pagination with our PHP Pagination class. Pagination
        class
        helps to generate the pagination links and get the records from the database using Ajax.
    </div>


@code {
    protected override async void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            await jsRuntime.InvokeVoidAsync("renderjQuery");
        }
        base.OnAfterRender(firstRender);
    }
}

[script.js]

function renderjQuery() {
    $('a[href*=\\#]:not([href=\\#])').on('click', function () {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.substr(1) + ']');
        if (target) {
            $('html,body').animate({
                scrollTop: target.offset().top
            }, 1000);
            return false;
        }
    });
}

View Sample in GitHub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.