views:

26

answers:

2

Creating this theme, I'm pretty new to jquery and struggling as usual.

Just watched this tutorial and decided to have a shot at making my own theme settings to simplify customization.

This is what i have so far:

(function($){

        $.fn.themeSettings = function(options) {

                var
                  slideshow = {
                        opacity: '0.5'
                  },
                  settings = $.extend({}, defaults, options);         
        };

        var slideShowShadow = $('#slideShadowTop, #slideShadowBottom, #slideShadowLeft, #slideShadowRight');

        slideShowShadow.css({ 
                opacity: slideshow.opacity 
        });

})(jQuery);

Any help would be awesome since It's messing up all over current jquery on the theme which I have not added to it yet.

I think by the code you can see what I'm trying to accomplish but obviously since I'm new to jQuery I'm not sure how to go about it.

Any help would be much appreciated, cheers

A: 

The way you have the plugin set up right now, slideShowShadow.css is trying to assign slideshow.opacity at loading time and outside the scope of the themeSettings function, which means that slideshow.opacity will be undefined. All you have to do is move the last few lines into the themeSettings function so that they're run when you apply the plugin to some element by calling $('#your_elem').themeSettings():

(function($){
  $.fn.themeSettings = function(options) {

    var slideshow = {
          opacity: '0.5'
        },
        settings = $.extend({}, slideshow, options),
        slideShowShadow = $('#slideShadowTop, #slideShadowBottom, #slideShadowLeft, #slideShadowRight');

    slideShowShadow.css({ 
      opacity: settings.opacity 
    });
  };
})(jQuery);

Note: Your original post referenced an undefined variable (default) in the $.extend function, which I'm sure you had intended to be the slideshow object.

andymism
Ahh I get it now, somewhat.
Daryl
How do I make it so the opacity works then, It's stopped screwing up the rest of my code which I understand why and how it works. But is opacity: slideshow.opacity the correct call? if it is, it isnt working :/
Daryl
My intention isn't really a 'plugin' its more so easy options for the theme I'm making so there wouldnt really be a document ready fucntion on the front end with options, i want it so at the top of the custom JS file theres easy option. Is this the right way to go about it?
Daryl
Yes, this is the way to do it. I called it a plugin because this is also the route you would take to create a general purpose plugin.
andymism
Daryl, I corrected the code in my example. It should be `opacity: settings.opacity` since you want the result of the merged options object. So now you should be able to specify the opacity as an argument and it should do what you expect.
andymism
Yea ive tried that already lol. None of it works lol. I'm guessing its because its not a ready function? not sure?
Daryl
You said here "All you have to do is move the last few lines into the themeSettings function so that they're run when you apply the plugin to some element by calling $('#your_elem').themeSettings()" What i'm aiming for is them to run anyway regardless of calling via an element.
Daryl
In that case you could just call it against an empty jQuery collection like this: `$().themeSettings()`. But yes, this is currently dependent on the #slideShadowTop, #slideShadowBottom, #slideShadowLeft, #slideShadowRight elements being available in the DOM, so something like this may be what you want: `$(document).ready(function () { $().themeSettings(); });`
andymism
I already worked it out mate, I appreciate all the effort you went through though, it helped me a lot to discover what I was trying to achieve! :)
Daryl
A: 
$(function(){

        var slideshow = {  
                opacity: 1,
                corners: true,
                shadows: true
        },
            xShadow = {
                opacity: 0.5      
        }; 

        // Slideshow Corners & Shadows        
        var $hWrap = $('#headerTopWrapper');

        if(slideshow.corners){
            $hWrap.prepend('<div id="slideCornersTop"></div>' + "\n" +
                           '<div id="slideCornersBottom"></div>');   
        }

        if(slideshow.shadows){
            $hWrap.prepend('<div id="slideShadowTop"></div>' + "\n" +
                           '<div id="slideShadowBottom"></div>' + "\n" +
                           '<div id="slideShadowRight"></div>' + "\n" +
                           '<div id="slideShadowLeft"></div>'); 
        }


        // Slideshow Shadow Opacity
        var $slideShowShadow = $('#slideShadowTop, #slideShadowBottom, #slideShadowLeft, #slideShadowRight');

        $slideShowShadow.css({
            opacity: xShadow.opacity
        });

});
Daryl