What improvements can I make to enhance the functionality of this carousel?

Check out the demo here.

The purpose of this script is to automatically adjust image sizes based on window size, ensuring consistent proportions across different screen resolutions.

Here are some challenges I have encountered with it and I am seeking solutions to improve its elegance and smoothness:

  • While it works well in larger resolutions (1280+), issues arise when viewed in 1024 or lower resolutions.
  • Scrolling behavior is not as smooth as desired.
  • Issues with alignment, such as white border disappearance in '3-up' or '4-up' views at 1680 resolution.
  • Misalignment occurs when reaching the end of a scroll in '4-up' view.
  • There is repetitive code that needs refining for multiple views.
  • Initial loading time takes approximately 3 seconds, showing smaller versions before resizing them, which appears untidy.
  • Seeking general optimization tips for smoother functionality.

You can view the relevant parts below, or examine the source code on the feedback page:

HTML:

<div id="compare_view" align="center">
    <!-- HTML content omitted for brevity -->
    </div>
    

CSS:

#slider-code { 
        /* CSS styles omitted for brevity */
    }

    /* Additional CSS rules omitted for brevity */

    #compare_view .compv-navbar img {
        /* CSS styles omitted for brevity */
    }
    

JS:

/* JS code omitted for conciseness */
    

I placed the JavaScript inside the window.load function to ensure proper detection of window size for image resizing. Your input and suggestions would be greatly appreciated!

Thank you.

Edit: Looking for assistance to resolve new issues after implementing suggested changes.

Edit 2: Facing challenges with recent modifications, seeking guidance to restore functionality. Visit the link above to see the updated version.

Answer №1

While this code is completely untested and a bit of a stab in the dark, it may offer some assistance with organizing your code and potentially improving performance through caching repetitive selectors.

However, please note that it may not resolve your specific issue related to the 4-up problem.

//Declare global variables
var oImg, sliderCode, sliderViewPort,sliderOverViewImg , win_width;
var oImg_height, oImg_width, oImg_ratio, tImg_width, tImg_height, sliderCode_w, sliderCode_h, ul_width;
var rImg_width, rImg_height, view_new_w, view_new_h, li_w, rUl_width;

function setUp(numImages) {
    oImg_height = oImg.height(); 
    oImg_width = oImg.width();
    oImg_ratio = oImg_height / oImg_width; 
    tImg_width = (win_width * 0.915) / num_of_images; 
    tImg_height = tImg_width * oImg_ratio;  
    sliderCode_w = sliderCode.width();
    sliderCode_h = sliderCode.height();
    var ul_width = $('#slider-code .viewport ul').width();

    // Code for resizing images
    $('#slider-code .viewport .overview img:lt(26)').css({ 'width': tImg_width, 'height': tImg_height });   

    rImg_width = sliderOverViewImg.width(); 
    rImg_height = sliderOverViewImg.height(); 

    // Adjust elements based on resized image dimensions
    $('#slider-code .next').css({ 'top': rImg_height / 2 });
    $('#slider-code .prev').css({ 'top': rImg_height / 2 });
    sliderCode.css({ 'width': '100%', 'height': rImg_height + 10 });
    sliderViewport.css({ 'width': win_width * 0.94, 'height': rImg_height + 10 });
    $('#slider-code .overview li').css({ 'width': rImg_width + 5 });
    view_new_w = sliderViewPort.width();
    view_new_h = sliderViewPort.height();
    li_w = $('#slider-code .overview li').width();
    rUl_width = $('#slider-code .viewport ul').width(); 

    // Initialize TinyCarousel plugin
    sliderCode.tinycarousel({ controls: true, animation: true, display: 1 });

}

$(window).load(function() {
    // Cache common elements
    oImg = $('#slider-code .viewport .overview img:eq(1)');
    sliderCode = $('#slider-code');
    sliderViewPort = $('#slider-code .viewport');
    sliderOverViewImg = $('#slider-code .viewport .overview img:eq(1)');

    win_width = $(window).width();
    num_of_images = 2; 

    // Event handler for changing number of displayed images
    $("#viewbar img").click(function(e) {
        num_of_images = parseInt($(this).attr("data-id"), 10); 
        $('#viewname').text(num_of_images + '-up');

        setUp(num_of_images);

        e.preventDefault();
    });

    // Default setup
    setUp(num_of_images);

}) 

Best of luck!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

FireFox - Dynamic Blinking Divs on Hover

It seems like in FireFox and IE, my hover effect is blinking for some reason. I can't figure out why this is happening. Should I switch to using JavaScript for my hovers or is there an easier CSS solution? Here's a JSFiddle link to demonstrate th ...

Finding the vertical dimension of an embedded HTML element

Trying to embed an HTML file using the object tag with external style sheets, but nothing seems to work. First question is how to do that? Note: style sheets are linked to both documents. Second question is about setting the height attribute of the object ...

Veracode Scan finds vulnerability in jQuery html method with Improper Neutralization of Script-Related HTML Tags in a Web Page error

Veracode has flagged the issue Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS) within the following line of code. $('#SummaryDiv').html(data); $.ajax({ url: 'Target_URL', type: &a ...

Error: autocomplete is not defined for this element

I've encountered an issue while trying to implement an autocomplete form. For some reason, I keep receiving a null error message regarding this.element. Below is the JavaScript code: //autocomplete function AutoComp() { new Ajax.Autocompleter("a ...

Perform an operation in JQuery after a different operation has been completed

Recently, I came across this JavaScript function that displays a loading image for a few seconds before revealing a hidden div: <script type="text/javascript> $(document).ready(function () { $('#load').html('<img style="display ...

What is the reason behind the removal of the "disabled" attribute when setting the "disabled" property to false?

Initially, I have a disabled button: <button disabled="disabled">Lorem ipsum</button> When using button.getAttribute('disabled'), it returns "disabled". But once the button is enabled with JavaScript: button.disabled = false; The ...

Introduction to Angular controller binding for beginners

I'm currently going through this tutorial : http://www.youtube.com/watch?v=i9MHigUZKEM At 46:32 minutes into the tutorial, this is the code I have implemented so far : <html data-ng-app="demoApp"> <body data-ng-controller="SimpleControlle ...

Directing communication in AngularJS: From controller to directive

I have a query about how a controller communicates with a directive created within that same controller. I am specifically interested in determining the recommended or best approach for this scenario. There are three possible approaches that come to mind: ...

Retrieve the URL string from HTML content with regular expressions

I am having trouble extracting the verification URL from an HTML file using regex. Unfortunately, I haven't been able to find any matches so far. Despite trying various patterns, none seem to be working for me. Any assistance or guidance on where I m ...

Sorting images can be quite a challenge

Struggling to organize images with jQuery. Reviewed http://jqueryui.com/demos/sortable/#display-grid, and it's functional. The issue arises when sorting images left to right, causing the form to shift left (but not vice versa). Despite adjusting margi ...

Techniques for transferring PHP print_r arrays to Javascript

Array ( [0] => Array ( [sno] => 1 [name] => Sivamani [contact] => 750241378 [$city] => Madurai ) ) Array ( [1] => Array ( [sno] => 2 ...

Create styles that reveal a div element upon hovering over a nested anchor link

When a user hovers over a link, I want to display a div box. For example: If there is an image inside a href, I would like a div to appear above it with the text 'Click here'. This is the React style code: a: { '& :hover':{ ...

Incorporating a new button into the edit menu of a SharePoint 2010 webpart

Can a link or button be included in the menu that pops up when the top left corner of a web part in sharepoint 2010 is clicked on? ...

Send a POST request including an audio file and then navigate to a different page

Here is the functionality I have implemented: ./upload: This feature allows users to upload an existing local audio file to ./post_receiver via a POST request. ./post_receiver: Receives the POST request and responds with an HTML page. ./record: Users ca ...

Heroku is unable to locate a module

Currently, I am in the process of creating a web application using meteor and Heroku. Interestingly, when testing my code locally, everything runs smoothly without any issues. However, once I proceed to deploy it on Heroku, I encounter the following errors ...

What is the best way to change the radio button selection using .val() in jQuery?

Recently, I encountered a challenge where I needed to select an input based on its value among four radio inputs. After the user clicks "next," the selected answer is saved in the AnsW array. However, when the user decides to click "back," I want the radio ...

Troubleshooting jQuery's Fading Effect

I am currently working on creating a stylish validation response for my login box with a minimalistic design. Due to limited space, I decided to change the word "Login" to display "Invalid Login. Forgot Password?" in a smooth fading animation. Initially, I ...

Flow - secure actions to ensure type safety

Currently implementing flow and looking to enhance the type safety of my reducers. I stumbled upon a helpful comment proposing a solution that seems compatible with my existing codebase: https://github.com/reduxjs/redux/issues/992#issuecomment-191152574 I ...

Tips on using htmlspecialchars in combination with a href:To safely include links in

If I have the following code: $text = "please visit this <a href='http://example.com'>site</a>." $text = htmlspecialchars($text); echo $text; The output will be please visit this <a href='http://example.com'>site< ...

My goal is to adjust the height of every text box on the page

Seeking guidance here. Within the webpage I'm developing, there are multiple 'Divs' containing 'text boxes' and 'background images' within them. I aim to adjust the height of each text box on the page. The height of each ...