Linking or scrolling to a specific content within a section

Linking or scrolling to a specific content within a section

by Thomas Müller -
Number of replies: 10

Hello everyone!

I have a minor question that makes me major pain. 

But maybe someone of you has an answer at hand to make it work.

I've made a floating button on my moodle page to alert the students on an event. When they click on the button it should show a text page which is located in one section of the course. I've made it work so far but it will only collapse the given section. As the content I am linking to is further down within the section, the students have to scroll down to bring the content up.

Is there a way to not only link to a given section but also the given content mark?

I've tried different ways but none of them works.

The URL given is like https://xxx.xxx.com/course/view.php?id=52&section=2.

I've tried to specify the with the data-section attribute but it doesn't work. I've also tried to use the name of the text field with an # attribute at the end but also no success.

So at the end the output by clicking the link should be, that it collapses the section and jumps or scrolls down to the content that shall be shown.

Would be great if someone has an idea!!!

Average of ratings: -
In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
https://xxx.xxx.com/course/view.php?id=52#section-2

It's #, not &... and a hyphen, not an equals.
 
You don't say what version and so on but you can typically copy the link from the left-hand navigation as that lists the topics in the course. 
 
Topic link
Average of ratings: Useful (2)
In reply to Howard Miller

Re: Linking or scrolling to a specific content within a section

by Thomas Müller -
Thank you for your answer.

We are on Moodle 4.1 and using the tile format.

With my link it opens up the tile but does not scroll to the section.
When I am using your suggestion (which makes sense to me) it will not even open up the tile....
In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I'm not especially familiar with the Tiles format but I recall that the very similar Grid format had the same limitation. The user actually had to click the tile/grid to fire the javascript which opened the topic section. Supplying the link to the section did not open the section. I'm guessing that this is probably because it's hard to do - that must be bypassing the course format code.

Anyway, my point is that this is (almost certainly) a limitation of the course format you are using rather than core Moodle. I would definitely make sure you have the latest version of Tiles (in case this is sorted) and, if it isn't, look for or make a report in their bug tracker.

https://bitbucket.org/dw8/moodle-format_tiles/issues
Average of ratings: Useful (2)
In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Thomas,

Have you asked David Watson, the maintainer (creator?) of the "Tiles format" plugin, to help you?

In the meantime, here is a workaround. Let's suppose you have a button in Tile 1, let's call it #myButton, and you want to be able to go to a certain place in Tile 2 by clicking on this button.

To identify the place you want to go in Tile 2, which we will call #myTarget, you need to use the id attribute on an HTML element. The simplest way is to add a "Text and media area" resource containing, for example, the following code:

<span id="myTarget"></span>

You are right, the URL is indeed "https://xxxx.com/course/view.php?id=52&section=2". However, we can reach Tile 2 more smoothly with the following script placed in Tile 1:

<button id="myButton">Go to Tile 2</button>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("#myButton").click(function(){

        // Store the fact that #myButton was clicked.
        sessionStorage.setItem("goto", "myTarget");

        // Trigger click on Tile 2.
        $(".tile-clickable#tile-2").click();
    });
});
</script>

Place the following script in Tile 2, say in its title:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
setTimeout(function() {

    // Get whether #myButton was clicked on not.
    let y = sessionStorage.getItem("goto");

    // If #myButton was indeed clicked.
    if (y == "myTarget") {
        // Go to #myTarget down in Tile 2.
        window.location.href = "https://xxxx.com/course/view.php?id=52#myTarget";
        // Reset #myButton.
        sessionStorage.setItem("goto", "top");
    }
// Wait 1 second (1000 milliseconds) before the above takes place.
// If your system is slow, you may want to increase to 2000 (i.e. 2 seconds) or more.
// There may be a better way to do this, if you are interested.
}, 1000);

</script>

In case students go directly to Tile 2 without pressing #myButton in Tile 1, you can insert the following link, say in the title of Tile 2, which will also allow students to directly reach #myTarget:

<a href="https://xxxx.com/course/view.php?id=52#myTarget">Go to myTarget</a>

Let me know if this works for you.

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Linking or scrolling to a specific content within a section

by Thomas Müller -
Good morning Dominique!

That workaround was very helpful and I think that could lead to the solution.
However, maybe you can help me fixing my code a littlebit so it will work from what it looks like:

I have placed a "floating image" on the start page, says "Save the Date". So that is somehow the "button"...

It is implemented with the following code:

<a id="ref1" href="https://xxx.xx.xxx.xxxx.de/course/view.php?id=52#myTarget">
    <div style="position: fixed; top: 360px; left: 80px;z-index: 9999;">
        <img style="width: 200px; height: 200px;" src="https://xxx.xx.xxx.xxxx.de/draftfile.php/13439/user/draft/776869727/20240614110724ynz6.png">
    </div>
</a>

It floats on the left side of the course.

I came that far that it will jump to the section of tile 2 but only if tile 2 is already open. So that works so far.

I think I need to combine the above code with the first part of your code. But I stuck where to place what...

If you have a further idea on that I would really appreciate your help.

Thank you so much so far!

In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello Thomas,

I am not sure what you mean by the start page, but I will assume below that your 'floating image button' is displayed on the same web page as Tile 2.

I suggest not using the <a> tag and instead using a small script:

<div id="myFloatingImageButton" style="position: fixed; top: 360px; left: 80px; z-index: 9999;">
    <img style="width: 200px; height: 200px;" src="https://xxx.xx.xxx.xxxx.de/draftfile.php/13439/user/draft/776869727/20240614110724ynz6.png">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $("#myFloatingImageButton").click(function(){
        // Store the fact that #myButton was clicked.
        sessionStorage.setItem("goto", "myTarget");
        // Trigger click on Tile 2.
        $(".tile-clickable#tile-2").click();
    });
});
</script>

In the title of tile 2, place the following code (repeated from my previous post):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
setTimeout(function() {
    // Get whether or not #myButton was clicked.
    let y = sessionStorage.getItem("goto");
    // If #myButton was indeed clicked.
    if (y == "myTarget") {
        // Go to #myTarget down in Tile 2.
        window.location.href = "https://xxxx.com/course/view.php?id=52#myTarget";
        // Reset #myButton.
        sessionStorage.setItem("goto", "top");
    }
// Wait 1 second (1000 milliseconds) before the above takes place.
// If your system is slow, you may want to increase to 2000 (i.e. 2 seconds) or more.
// There may be a better way to do this, if you are interested.
}, 1000);
</script>

Don't forget to place an element with the id 'myTarget' at the location you want to reach in tile 2, for example:

<span id="myTarget"></span>
Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Linking or scrolling to a specific content within a section

by Thomas Müller -
Thank you for your effort, but it will not work as it should...

I am not sure what you mean by the start page...
 
The button is implemented at the Overview-Section, so it shows up on the main course page.
The idea was to open up tile 2 and jumps to the object (which is a simple text field that shows up a graphic with a date reminder. 
 
Your way opens up tile 2 but it will not jump to the section within tile 2.
 
 
In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Thomas Müller -
I have also tried to work with the scrollIntoView() Method but also without any luck
In reply to Thomas Müller

Re: Linking or scrolling to a specific content within a section

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
  • Did you put the second script in the HTML of the title of Tile 2? Did you try with 2000 instead of 1000 on the last line of the script?

  • Did you put <span id="myTarget"></span> in the HTML of the 'simple text field that shows up a graphic'?

I am currently out of the office. We can resume this conversation in about two weeks.

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Linking or scrolling to a specific content within a section

by Thomas Müller -
Yes, followed all your suggestions.

Thank you for your effort. I will try to find a solution and looking forward if I need your help. But for now enjoy the two weeks ooO.